我是一个android新手。我正在使用一个光标适配器和一个列表视图,其中有一个切换按钮和一个textview…当我点击切换按钮时,我需要获得点击事件和项目在列表中的位置。
我看到了很多线程,但我没有找到一个适合我的完美解决方案…
最后我决定在bindview中设置位置,并尝试在单击事件中检索它。但当我进入储物柜在点击事件的位置,我得到空指针异常。
请帮我这个家伙。谢谢你。
public class CalendarListAdapter extends CursorAdapter implements OnClickListener{
protected static class RowViewHolder {
public TextView mTitle;
public ToggleButton mButton;
public int position;
}
protected ListView calListView;
@SuppressWarnings("deprecation")
public CalendarListAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView title = (TextView) view.findViewById(R.id.calendarName);
System.out.println("cursor details"
+ cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))));
title.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))));
Log.d("click position " , "position is " +cursor.getPosition() );
ToggleButton button = (ToggleButton) view.findViewById(R.id.silentToggle);
button.setOnClickListener(this);
Log.d("click position " , "position is " +cursor.getPosition() );
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View calendarListView = inflater.inflate(R.layout.calendar_list_item, null);
RowViewHolder rowView = new RowViewHolder();
rowView.mTitle = (TextView)calendarListView.findViewById(R.id.calendarName);
rowView.mButton = (ToggleButton) calendarListView.findViewById(R.id.silentToggle);
rowView.position = cursor.getPosition();
rowView.mTitle.setOnClickListener(titleOnClickListener);
rowView.mButton.setOnClickListener(buttonOnClickListener);
return calendarListView;
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Log.d("item clicked", "clicked" );
RowViewHolder holder = (RowViewHolder)view.getTag();
int position = holder.position;
Log.d("common clicked", "clicked -" +position );
}
private OnClickListener titleOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RowViewHolder holder = (RowViewHolder)v.getTag();
int position = holder.position;
Log.d("title clicked", "clicked -" +position );
}
};
private OnClickListener buttonOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RowViewHolder holder = (RowViewHolder)v.getTag();
int position = holder.position;
Log.d("button clicked", "clicked -" +position );
}
};
}
要能够使用v.g gettag(),您需要两个东西。在newView中你需要做
calendarListView.setTag(rowView);
和在bindView中,你需要将正确的数据插入到holder中:
rowView.position = cursor.getPosition(); // (same as in newView)
要使所有这些协同工作,您还需要确保您正在执行getTag()的视图是您执行setTag的视图。
在您的情况下,这意味着在newView:
中执行此操作rowView.mTitle.setTag(rowView);
rowView.mButton.setTag(rowView);
根本不使用RowViewHolder可能会更简单。而是这样做:
在bindView:int pos = cursor.getPosition();
title.setTag(pos);
button.setTag(pos);
然后在OnClickListeners中输入
Integer pos = (Integer) v.getTag();
祝你好运!
上面的setTag和getTag的答案对我来说很好,但不知怎的,我意识到标签上的数字并不总是正确的。检查后,我发现我的newView功能工作太多次,这就是为什么我所有的标签都是"0"。如果你有这个问题,检查一下