tView控件setOnItemClickListener不工作

tView控件setOnItemClickListener不工作

本文介绍了ListView控件setOnItemClickListener不工作,加入按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图每行文字和按钮,列表视图setOnItemClickListener()无法正常工作。是否有可能处理项目,并单击按钮单击事件不同(产品Click应该叫ActivityA和按一下按钮应该叫ActivityB)。有没有人有一个解决方案

 私人的ArrayList<字符串> userIDArr = NULL;
    私人的ArrayList<字符串> userNameArr = NULL;
    私人DatabaseHelper dbHelper = NULL;
    私人的ListView userListView = NULL;


    公共无效的onCreate(包savedInstanceState)
        {
          super.onCreate(savedInstanceState);
          的setContentView(R.layout.list_view);
          dbHelper =新DatabaseHelper(this.getApplicationContext());
          地图<字符串,ArrayList的<字符串>> displayMap = dbHelper.getUserListToDisplay();
          userIDArr = displayMap.get(用户名);
          userNameArr = displayMap.get(FirstName1);


          userListView =(ListView控件)findViewById(R.id.listView2);
          userListView.setAdapter(新UserListAdapter(这一点,userIDArr));


          userListView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
              @覆盖
              公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,长ARG3){

                  Toast.makeText(usersListActivity.this,
                            项目的位置+位置+点击,Toast.LENGTH_LONG).show();
              }
            });
     }


    公共类UserListAdapter扩展ArrayAdapter<字符串>
    {
        活动范围内;
        公共UserListAdapter(活动背景下,ArrayList的<字符串>的名字){
            超(背景下,R.layout.list_item,姓名);
            this.context =背景;
        }
        私有类ViewHolder {
            公众的TextView UserNameAndID;
            公共TextView的说明;
            公众按钮UploadBtn;
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            ViewHolder持有人;
            查看rowView = convertView;
            如果(rowView == NULL){
                LayoutInflater充气= context.getLayoutInflater();
                rowView = inflater.inflate(R.layout.list_item,空,真正的);
                持有人=新ViewHolder();
                holder.UserNameAndID =(TextView中)rowView.findViewById(R.id.User_detailsTxt);
                holder.Description =(TextView中)rowView.findViewById(R.id.User_status);
                holder.UploadBtn =(按钮)rowView.findViewById(R.id.uploadbutton);
                holder.UploadBtn.setOnClickListener(新View.OnClickListener(){

                        公共无效的onClick(视图v){
                        Toast.makeText(usersListActivity.this,单击按钮,Toast.LENGTH_SHORT).show();
                        }
                    });
                    rowView.setTag(保持器);
            } 其他 {
                支架=(ViewHolder)rowView.getTag();
            }
            字符串s = userNameArr.get(位置)+,+ userIDArr.get(位);
            holder.UserNameAndID.setText(多个);
            holder.Description.setText(U R IN中间);
            返回rowView;
        }
    }
}`
 

解决方案

设定好尝试你的按钮(或要处理单击列表项中的任何其它视图)是这样的:

 机器人:可调焦=假
机器人:focusableInTouchMode =假
 

我没有测试这一点,但请让我知道如果这个工程。

I have a list view with text and button in each row, list view setOnItemClickListener() is not working. is it possible to handle item click and button click events differently(item click should call ActivityA and button click should call ActivityB). Does anyone have a solution

    private ArrayList<String> userIDArr = null;
    private ArrayList<String> userNameArr = null;
    private DatabaseHelper dbHelper = null;
    private ListView userListView=null;


    public void onCreate(Bundle savedInstanceState)
        {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.list_view);
          dbHelper = new DatabaseHelper(this.getApplicationContext());
          Map<String,ArrayList<String>> displayMap = dbHelper.getUserListToDisplay();
          userIDArr = displayMap.get("UserID");
          userNameArr = displayMap.get("FirstName1");


          userListView = (ListView) findViewById(R.id.listView2);
          userListView.setAdapter(new UserListAdapter(this,userIDArr));


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

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


    public class UserListAdapter extends ArrayAdapter<String>
    {
        Activity context;
        public UserListAdapter(Activity context, ArrayList<String> names) {
            super(context, R.layout.list_item, names);
            this.context = context;
        }
        private class ViewHolder {
            public TextView UserNameAndID;
            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.UserNameAndID = (TextView) rowView.findViewById(R.id.User_detailsTxt);
                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(usersListActivity.this," Button clicked",Toast.LENGTH_SHORT).show();
                        }
                    });
                    rowView.setTag(holder);
            } else {
                holder = (ViewHolder) rowView.getTag();
            }
            String s = userNameArr.get(position)+","+userIDArr.get(position);
            holder.UserNameAndID.setText(s);
            holder.Description.setText("U r in middle");
            return rowView;
        }
    }
}`
解决方案

Try seting your buttons(or any other views you want to handle click inside a list item) like this:

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

I didn't test this, but please let me know if this works.

这篇关于ListView控件setOnItemClickListener不工作,加入按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:56