3

我有一个ListView列表中的每个元素都包含一个 TextView 和两个不同的按钮。像这样的东西:

ListView
--------------------
[ImageView][Text][CheckBox][Button]
--------------------
[ImageView][Text][CheckBox][Button]
--------------------
... (and so on) ...

OnItemClickListener使用此代码,我可以为整个项目创建一个:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> list, View view, int position, long id) {
        Log.i(TAG, "onListItemClick: " + position);

        }

    }
});

但是,我不希望整个项目都是可点击的,而只是每个列表元素的复选框和按钮。

所以我的问题是,如何使用以下参数为这两个按钮实现 onClickListener:

  • int id(与列表中的每个项目相关联的一些 id)
  • int position(这是列表中发生按钮单击的元素)
4

7 回答 7

1

我以这种方式解决了同样的问题:在 ListViewItem 的 layout.xml 中给元素一个标签,您要在其中添加 OnClickListener:

       android:id="@+id/CheckBox"
       android:tag="CheckBox" />

……

       android:id="@+id/Button"
       android:tag="Button" />

在代码中,您的 ListView 被夸大了,使用以下代码添加 OnClickListener:

       listView.findViewWithTag("CheckBox").setOnClickListener(createCheckBoxOnClickListener(act));
       listView.findViewWithTag("Button").setOnClickListener(createButtonOnClickListener(act));
于 2014-01-01T23:39:52.350 回答
1

你需要制作 baseAdapter 来实现这个

public class ContactsAdapter extends BaseAdapter {

    ArrayList<ContactInfo> mlist;
    Context mcontext;


public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {      
        mlist =  mchtlist;
        mcontext = context;

    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int postion) {
        return mlist.get(postion);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
        public View getView(int position, View convertview, ViewGroup viewgroup){
            View view = null;
            if(convertview == null){
                LayoutInflater inflater = context.getLayoutInflater();
                view = inflater.inflate(R.layout.contactrow, null);

                ContactHolder holder = new ContactHolder();

                holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
                holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
                holder.chkselected = (CheckBox)view.findViewById(R.id.check);

                setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View arg0) {
            // to open the selected file in resp

                  // do your work here
                 }});


    chkselected .setOnClickListener(new OnClickListener() {
        @Override
    public void onClick(View v) {
    // Toast.makeText(context,// "checked is clicke="+pos, 12).show();
            if (chkselected.isChecked())          
                       {            

                        // do your work here
            } else {

     // do your work here                               
            }
        }
});



            view.setTag(holder);

        }
            else{
                view = convertview;
            }
            ContactHolder holder2 = (ContactHolder) view.getTag();
            holder2.txtviewfirstname.setText(list.get(position).firstname);
            holder2.txtviewphone.setText(list.get(position).phonenumber);
            holder2.chkselected.setChecked(list.get(position).selected);
            return view;
        }

}
于 2012-09-04T12:50:40.163 回答
0

您必须getView在适配器中的方法中执行此操作..

于 2012-09-04T12:49:24.253 回答
0

不要为列表视图添加 onItemClickListener 而是为您所需的按钮添加 onClick 并为复选框这样做

 CheckBox cbox = new CheckBox(this);// find your checkbox from the view here.
        cbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ // do what ever you want. } } });
于 2012-09-04T12:54:39.097 回答
0

很简单,只需OnClickListener为这两个按钮实现,并获取您从中获取的位置,getView()确保将位置设置为final以获取它OnClickListener

于 2012-09-04T12:50:27.747 回答
0

您可以在适配器的 getView() 方法中添加侦听器。

于 2012-09-04T12:50:57.260 回答
0

检查下面的代码它可能会帮助你。

private class ListViewAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public ListViewAdapter(Context con) {
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(con);
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return main_genral_class.review_name.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub

            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ListContent holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.review_row, null);
                holder = new ListContent();

                holder.img = (ImageView) convertView
                        .findViewById(R.id.imageView1);
                holder.name = (TextView) convertView
                        .findViewById(R.id.textView1);
                holder.check_date = (TextView) convertView
                        .findViewById(R.id.textView2);




                holder.img.setOnClickListener(mOnTitleClickListener);
                holder.name.setOnClickListener(mOnTitleClickListener1);
                holder.check_date.setOnClickListener(mOnTitleClickListener2);


                convertView.setTag(holder);
            } else {

                holder = (ListContent) convertView.getTag();
            }

            holder.text2.setText(main_genral_class.review_shout.get(position));
            return convertView;
        }
    }

private OnClickListener mOnTitleClickListener = new OnClickListener() {
        public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v
                    .getParent());

                Toast.makeText(review_activity.this, "click on Image View",
                        Toast.LENGTH_SHORT).show();

        }
    };

private OnClickListener mOnTitleClickListener1 = new OnClickListener() {
            public void onClick(View v) {
                final int position = mListView.getPositionForView((View) v
                        .getParent());

                    Toast.makeText(review_activity.this, "click on Text View",
                            Toast.LENGTH_SHORT).show();

            }
        };

private OnClickListener mOnTitleClickListener2 = new OnClickListener() {
            public void onClick(View v) {
                final int position = mListView.getPositionForView((View) v
                        .getParent());

                    Toast.makeText(review_activity.this, "click on Date Text View",
                            Toast.LENGTH_SHORT).show();

            }
        };
于 2012-09-04T13:11:24.387 回答