SimpleCursorAdapter中的按钮 [英] Button inside SimpleCursorAdapter

查看:88
本文介绍了SimpleCursorAdapter中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a ListView that I populate with a SimpleCursorAdapter. For each item in my list i've, in this order: TextView > RatingBar > TextView > ImageView. I want to add a button on the ImageView but I dont know how to this...

我在其中填充ListView的地方:

Where I populate the 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:

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

以及用于醉酒的XML:

And the XML for beerdrunk:

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

但是它不起作用...我怎么知道我的行"中单击了哪个视图?

But it does not work... How can i do to know which view is clicked inside my "row" ?

推荐答案

如果要单击图像,请使用

If you want to click on an image, use an ImageButton instead of an ImageView.
You can then set the onClick attribute on the button that calls a method in your Activity.

扩展 SimpleCursorAdapter 并在bindView方法中添加每个按钮的onClick侦听器. (也许让它在您的活动中调用方法... public void buttonClicked(int position, View v)...,以便您知道单击了哪一行和哪个视图.

Extend a SimpleCursorAdapter and in the bindView method add onClick listeners for each button. (Maybe make it call a method in your activity ... public void buttonClicked(int position, View v)... so you know which row and which view was clicked.

以下是您可以使用的 CursorAdapter 的示例实现.

Here is an example implementation of CursorAdapter you could use.

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

}

;)

这篇关于SimpleCursorAdapter中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆