2

我正在使用扩展CursorAdapter以在listview. 为了显示特定的电话号码,我想要一个 id,所以我尝试在点击事件中获取它。我在绑定方法中设置了点击监听器如下

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

我没有从getId()通话中获得正确的视图 ID,因此无法从数据库中获取电话号码。

完整的代码在这里:

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);

        }
       });
  }
4

2 回答 2

0

不要使用 view.id 来存储数据/参考。使用view.tag你可以存储任何类型的对象。

 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 !!
         }

    }
   });
于 2014-05-06T07:42:11.163 回答
0

您的 v1 在哪里声明?但是这样想:你在列表视图中点击你的视图。方法 onClick() 被调用,但 v1 具有最后分配给它的值,而不是在为该特定视图执行 bindView() 时分配给它的值。一种解决方案是将 id 放在视图的标记属性中。

于 2012-06-18T15:43:41.130 回答