I asked this question yesterday, but I wrote it far more complicated than I think this problem is. I am very confused and I found some very similar questions here, but neither of them solved my problem. So I have a listview, every listview contains an image, a textview and an edittext, but only the edittext is important.
If I click on the textview, I want to log the content of it for testing. My problem is if the user changes it then after the change I log the same thing. So changing it in the emulator and not using setText, it does nothing. I have a database, in every of the listview there is an edittext (holder.quant).
class MyAdapter extends ArrayAdapter
{
LayoutInflater inflat;
ViewHolder holder;
public MyAdapter(Context context, int textViewResourceId,
ArrayList<EachRow> objects)
{
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
inflat=LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
convertView=inflat.inflate(R.layout.row_checkox, null);
holder=new ViewHolder();
holder.textView=(TextView)convertView.findViewById(R.id.textView111);
holder.image=(ImageView)convertView.findViewById(R.id.imageView111);
holder.quant = (EditText)convertView.findViewById(R.id.quantity);
holder.image.setOnClickListener(CustomList.this);
convertView.setTag(holder);
}
holder=(ViewHolder) convertView.getTag();
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) v ;
String text_of_clicked_textview = tv.getText().toString();
final String[] ddata = text_of_clicked_textview.split(" "); //split the result using the spaces (so you could obtain the name, hotness and the other string you use)
final long yy = Long.parseLong(ddata[0]);
info.open();
info.updateQuan(yy, holder.quant.getText().toString());
Log.i("tagitagi", holder.quant.getText().toString());
info.close();
}
});
EachRow row= getItem(position);
EachRow row2= getItem2(position);
holder.quant.setText(row2.text);
holder.textView.setText(row.text);
NullPointerException
holder.image.setTag(position);
holder.quant.setTag(position);
return convertView;
}
@Override
public EachRow getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public EachRow getItem2(int position) {
// TODO Auto-generated method stub
return list2.get(position);
}
private class ViewHolder
{
TextView textView;
ImageView image;
EditText quant;
}
}
private class EachRow
{
String text;
boolean checkBool;
}
I tried addTextChangedListener, but it gives me an error when I use settext. So what is the problem?
Thanks in advance!
I managed to figure out how can I make it work. The holder.quant.gettext().toString does not work, even if I click on the textview of one of the rows I cannot log the content of the textview. The holder somehow blocks it, I will always log the same textview…
So I needed to create a new method:
The list contains the rows, the .text comes from the method:
and the definition of the holder:
So after this the onClick method:
So with this I can refer to the holder.quant or holder.textview or anything that is in a row. I still do not know why the simple holder.quant.getText().toString does not work, but with this, so refering to the position of a row will makes it work.