2

I am using custom adapter that extends CursorAdapter for displaying data in listview. To display particular phone number, I want an id so I try to get it on a click event. I have set the click listener in bind method as follows

right.setOnClickListener(new View.OnClickListener() 
   {
        @Override
        public void onClick(View v) 
        {
            a = v1.getId();
            String num=dh.getNumberFromId(a);
        }
   });

I am not getting the correct id of the view from getId() call and hence fail to get the phone number from db.

The full code is here :

public class CallSchedulerCustomAdapter2 extends CursorAdapter
{    
  DbHelper dh;

public Context con;
public LayoutInflater inflater;
int nameindex,phoneindex,emailindex,smsindex,msubindex,mbodyindex, column_id;
TextView text1,text2,text3;
int a;   String s1;



@SuppressWarnings("static-access")
public CallSchedulerCustomAdapter2(Context con, Cursor c  )
{
    super(con, c);
    // TODO Auto-generated constructor stub
    this.con = con;
    nameindex = c.getColumnIndex(dh.contactname);
    phoneindex = c.getColumnIndex(dh.contactnumber);
    emailindex = c.getColumnIndex(dh.contactmailid);
    smsindex=c.getColumnIndex(dh.contactsms);
    msubindex=c.getColumnIndex(dh.contactmailsub);
    mbodyindex=c.getColumnIndex(dh.contactmailbody);
    column_id= c.getColumnIndex("_id");
    this.inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

    public static Cursor c;
@SuppressWarnings("static-access")
@Override
public void bindView( View view,   Context context, Cursor cursor) 
{     

    // TODO Auto-generated method stub
    v1=view;
     c=cursor;
    text1= (TextView)view.findViewById(R.id.unschedulecontactnametv);
    text2= (TextView)view.findViewById(R.id.unschedulecontactnumbertv);
    text3= (TextView)view.findViewById(R.id.unschedulecontactmailidtv);
     String s = c.getString(cursor.getColumnIndex(dh.contactname));
       text1.setText(s);


    text1.setText(cursor.getString(nameindex));
    text2.setText(cursor.getString(phoneindex));
    text3.setText(cursor.getString(emailindex));
    View right = view.findViewById(R.id.callb1);
    dh = new DbHelper(context); 

    right.setOnClickListener(new View.OnClickListener() 
       {
        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
               a = v1.getId();
             String num=dh.getNumberFromId(a);

        }
       });
  }
Ron
  • 24,175
  • 8
  • 56
  • 97
user181291
  • 719
  • 1
  • 5
  • 7

2 Answers2

0

Where is your v1 declared? But think about it in this way: You click on yout view in listview. Method onClick() gets called but the v1 has a value that was last assigned to it, not the value that was assigned to it at the moment bindView() was executed for that particular view. One solution would be to put id in a tag property of a view.

Anderson
  • 1,011
  • 2
  • 11
  • 24
0

don't use the view.id to store data/ref. use view.tag you can store any kind of object.

 public void bindView( View view,   Context context, Cursor cursor) 
{     

MyDataHolder data = new MyDataHolder(cursor, "whatever");
 view.setTag(data);

....
}



right.setOnClickListener(new View.OnClickListener() 
     {
      @Override
      public void onClick(View v) 
      {

           int viewId = v.getId();
           MyDataHolder data = (MyDataHolder)v.getTag()
           if (R.id.xxxx == viewId) { // not a good idea
         }
           if (data.whatever == xxx) { // much better !!
         }

    }
   });
Loda
  • 1,970
  • 2
  • 20
  • 40