我们可以有两个按钮和onItemClick听众的ListView的android系统中? [英] can we have both of Button and onItemClick listener in ListView in android?

查看:127
本文介绍了我们可以有两个按钮和onItemClick听众的ListView的android系统中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有按钮 ListView控件中的每一行 onClickListener 按钮。我想补充 onItemSelectListener 我的的ListView 太。可能吗?如果是的话我该怎么办呢?
任何帮助将AP preciated。

I have Button in each row of ListView and onClickListener for Button. I want to add onItemSelectListener to my ListView too. Is it possible? If yes how can I do that?
Any help will be appreciated.

推荐答案

是的,它是可能的...

Yes it is possible ...

在创建自定义视图适配器列表视图,u必须添加onclicklistener按钮 和u还可以根据需要添加onItemSelectListener的列表视图。 它会工作。

While creating custom view adapter for listview, u have to add onclicklistener on button and u can also need to add onItemSelectListener on Listview. It would work.

使用列表视图code为

use listview code as

listView = (ListView) findViewById(R.id.listView2);
      listView .setAdapter(new CustomListAdapter (this,userIDArr));


      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

              Toast.makeText(Activity.this,
                        "Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
          }
        });

和创建适配器像

public class CustomListAdapter extends ArrayAdapter<String>
{
    Activity context;
    public CustomListAdapter (Activity context, ArrayList<String> names) {
        super(context, R.layout.list_item, names);
        this.context = context;
    }
    private class ViewHolder {

        public TextView Description;
        public Button  UploadBtn;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.list_item, null, true);
            holder = new ViewHolder();

            holder.Description = (TextView) rowView.findViewById(R.id.User_status);
            holder.UploadBtn = (Button) rowView.findViewById(R.id.uploadbutton);
            holder.UploadBtn.setOnClickListener(new View.OnClickListener() {  

                    public void onClick(View v) {  
                    Toast.makeText(Activity.this," Button clicked",Toast.LENGTH_SHORT).show();
                    }   
                }); 
                rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        holder.Description.setText("U r in middle");
        return rowView;
    }
}

现在处理单击列表项内使用低于code

Now to handle click inside a list item use the below code

android:focusable="false"
android:focusableInTouchMode="false"

设置这些线,同时创造按钮标签

set these lines while creating Button tag

它的工作... 请让我知道您的反馈。

It would work ... Please let me know your feedback..

这篇关于我们可以有两个按钮和onItemClick听众的ListView的android系统中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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