gpt4 book ai didi

android - SimpleCursorAdapter 中的按钮

转载 作者:行者123 更新时间:2023-11-29 00:31:49 28 4
gpt4 key购买 nike

我有一个用 SimpleCursorAdapter 填充的 ListView。对于我列表中的每一项,我按以下顺序排列:TextView > RatingBar > TextView > ImageView。我想在 ImageView 上添加一个按钮,但我不知道该怎么做...

我填充 ListView 的地方:

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

OnItemClickListener:

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

还有 beerdrunk 的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/champName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="130"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:maxLines="3"
android:padding="5dp"
android:paddingRight="20dp"
android:singleLine="false"
android:text="@string/beerBook"
android:textColor="@color/black1"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>

<View
android:layout_width="1.5dip"
android:layout_height="fill_parent" />

<RatingBar
android:id="@+id/champNote"
style="@style/beerRatingSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isIndicator="true"
android:numStars="5"
android:layout_margin="1.5dp"
android:rating="5"
android:stepSize="0.10" >
</RatingBar>

<View
android:layout_width="1.5dip"
android:layout_height="fill_parent" />

<TextView
android:id="@+id/champDrunk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="30"
android:gravity="right|center_vertical"
android:padding="5dp"
android:layout_marginLeft="5dp"
android:text="200"
android:textColor="@color/black1"
android:textSize="20sp"
android:textStyle="bold" />

<View
android:layout_width="1.5dip"
android:layout_height="fill_parent" />

<ImageView
android:id="@+id/champPlusone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_margin="5dp"
android:layout_weight="30"
android:gravity="right|center_vertical"
android:clickable="false"
android:src="@drawable/plus2" />
</LinearLayout>

我试过类似的方法:

    if(arg2 instanceof ImageView){
//do some stuff
}

但它不起作用...我怎样才能知道在我的“行”中单击了哪个 View ?

最佳答案

如果你想点击图片,使用 ImageButton而不是 ImageView。
然后,您可以在调用 Activity 中的方法的按钮上设置 onClick 属性。

扩展 SimpleCursorAdapter并在 bindView 方法中为每个按钮添加 onClick 监听器。 (也许让它在您的 Activity 中调用一个方法...public void buttonClicked(int position, View v)... 这样您就知道点击了哪一行和哪个 View 。

这里是 CursorAdapter 的一个例子你可以使用。

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

}

;)

关于android - SimpleCursorAdapter 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15146846/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com