我是一个Android新手..我正在使用光标适配器和listview,它有一个切换按钮和textview ...当我点击切换按钮时,我需要获取点击事件和项目的位置清单..
我看到很多线程,但我找不到适合我的完美解决方案......
最后我决定在bindview中设置位置并尝试在点击事件中检索它..但是当我在click事件中访问holder.position时,我得到空指针异常。
请帮助我这些家伙。非常感谢
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 );
}
};
}
答案 0 :(得分:9)
为了能够使用v.getTag(),你需要两件事。在newView中你需要做
calendarListView.setTag(rowView);
并且在bindView中你还需要将正确的数据插入到持有者中:
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();
祝你好运!
答案 1 :(得分:0)
上面的答案,setTag和getTag对我来说效果很好, 但不知怎的,我意识到标签并不总能带来正确的数字。 检查后,我发现我的newView函数工作太多次,这就是为什么我的所有标签都是“0”。如果你有这个问题,请检查 here寻求解决方案。