我正在使用扩展 CursorAdapter
的自定义适配器在 listview
中显示数据。为了显示特定的电话号码,我想要一个 id,所以我尝试在点击事件中获取它。我在绑定(bind)方法中设置了点击监听器,如下所示
right.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
a = v1.getId();
String num=dh.getNumberFromId(a);
}
});
我没有从 getId()
调用中获取正确的 View ID,因此无法从 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);
}
});
}
最佳答案
你的 v1 是在哪里声明的? 但是这样想: 您在 ListView 中单击您的 View 。方法 onClick() 被调用,但 v1 具有最后分配给它的值,而不是在为该特定 View 执行 bindView() 时分配给它的值。 一种解决方案是将 id 放入 View 的标记属性中。
关于android - 无法在 cursoradapter 的绑定(bind) View 覆盖中获取 View 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10275864/