android - SimpleCursorAdapter 内的按钮

标签 android android-layout simplecursoradapter

我有一个用 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(intposition, 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/

相关文章:

c# - 在 Visual Studio 中创建的 PCL 能否在 Xamarin 项目中使用?

java - 如何使线性布局出现在 GridView 中?

android - 正确获取 ListView 中行的 _id

android - 如何使用 SimpleCursorAdapter 使 ListView 跳过 ""文本?

java - Android Java - 获取 JSON 值

Android 自定义身份验证器允许访问屏幕

android - Google Play Console App Rejection APK 需要有效的隐私政策和重要的披露

android - Android LinearLayout不可见

Android 布局有 4 个正方形,每个正方形内有一个按钮

android - 当我一次又一次滚动 ListView 时应用程序崩溃