Android SimpleCrsorAdapter内的按钮
我有一个用SimpleCursorAdapter填充的列表视图。对于列表中的每一项,我都按以下顺序进行了操作:TextView>RatingBar>TextView>ImageView。我想在ImageView上添加一个按钮,但我不知道如何 我在其中填充ListView:Android SimpleCrsorAdapter内的按钮,android,android-layout,simplecursoradapter,Android,Android Layout,Simplecursoradapter,我有一个用SimpleCursorAdapter填充的列表视图。对于列表中的每一项,我都按以下顺序进行了操作:TextView>RatingBar>TextView>ImageView。我想在ImageView上添加一个按钮,但我不知道如何 我在其中填充ListView: adapter = new SimpleCursorAdapter(this, R.layout.beerdrunk, cursor, new String[] { "beers", "notes",
adapter = new SimpleCursorAdapter(this, R.layout.beerdrunk, cursor,
new String[] { "beers", "notes", "numbers" }, new int[] {
R.id.champName, R.id.champNote, R.id.champDrunk });
adapter.setViewBinder(binder);
list = (ListView) findViewById(R.id.listMyBeers);
list.setItemsCanFocus(true);
list.setOnItemClickListener(onClickBeer);
list.setAdapter(adapter);
监听器:
private OnItemClickListener onClickBeer = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println(""+ arg0);
System.out.println(""+ arg1);
System.out.println(""+arg2);
Intent newActivity = new Intent(MyBeers.this, DetailsBeer.class);
newActivity.putExtra("com.example.checkmybeer.DetailsBeer", ""
+ arg3);
startActivityForResult(newActivity, 0);
}
};
但它不起作用。。。如何知道在我的“行”中单击了哪个视图?如果要单击图像,请使用图像视图而不是图像视图。
然后,您可以在调用活动中的方法的按钮上设置
onClick
属性
扩展a,并在bindView
方法中为每个按钮添加onClick
侦听器。(可能让它调用活动中的一个方法…public void按钮单击(int position,View v)…
这样您就知道单击了哪一行和哪一视图
下面是一个您可以使用的示例实现
class CustomCursorAdapter extends CursorAdapter{
LayoutInflater inflater;
public CustomCursorAdapter(Context context, Cursor c, int flags){
super(context,c,flags);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor){
int row_id = cursor.get('_id'); //Your row id (might need to replace)
ImageButton button = (ImageButton) view.findViewById(R.id.champPlusone);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.beerdrunk, parent, false);
bindView(v,context,cursor);
return v;
}
}
;)“如何才能知道在我的“行”中单击了哪个视图?”“您需要在每个视图中创建一个自定义适配器并设置OnClickListeners,以便执行特殊操作。快速提问,为什么要在listview中添加一个按钮?”?与仅仅让用户点击行本身不同?@BrentHronik我想作者希望RatingsBar做一件事,按钮做另一件事,行本身可能做一些不同的事。好的,谢谢Sam的回答:)@BrentHronik,点击这一行做一些不同于点击同一行内的按钮的事情这和Sam的评论对我帮助很大!!:)
if(arg2 instanceof ImageView){
//do some stuff
}
class CustomCursorAdapter extends CursorAdapter{
LayoutInflater inflater;
public CustomCursorAdapter(Context context, Cursor c, int flags){
super(context,c,flags);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor){
int row_id = cursor.get('_id'); //Your row id (might need to replace)
ImageButton button = (ImageButton) view.findViewById(R.id.champPlusone);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.beerdrunk, parent, false);
bindView(v,context,cursor);
return v;
}
}