English 中文(简体)
不能在约束性观点中 get取一种观点,而不是曲线。
原标题:Unable to get id of a view in bindview override of cursoradapter

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

我没有从<代码>getId()打电话得到正确的意见,因此无法从db获得电话号码。

全文如下:

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

        }
       });
  }
问题回答

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.

不要使用这种观点。 d 用于储存数据。 http://developer.android.com/fer/android/view/View.html#getTag%2829”rel=“nofollow”view。 您可以储存任何种类的物品。

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

    }
   });




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签