在自定义适配器列表视图的单个项目上设置监听器

时间:2018-04-13 17:34:55

标签: android

我正在尝试从数据库填充自定义列表视图适配器,并在每个项目上设置onItemClickListener。但是我应该把听众放在哪里?当我将它放在主Activity类中时,它无法工作。

主要活动类:

clist=db.getAllContacts ();
myAdapter=new MyAdapter (MainActivity.this, (ArrayList<Contact>) clist);
lv.setAdapter(myAdapter);
lv.setOnItemClickListener (new AdapterView.OnItemClickListener () {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {

            //Some code to work on the Items
        }
    });

适配器类MyAdapter

public class MyAdapter extends ArrayAdapter<Contact> {
public MyAdapter(Context context, ArrayList<Contact> users) {
    super(context, 0, users);
}
public View getView(int position, View convertView, ViewGroup parent) {

    Contact user = getItem (position);

    if (convertView == null) {
        convertView = LayoutInflater.from (getContext ()).inflate (R.layout.listviewadapter, parent, false);
    }
    TextView name = convertView.findViewById (R.id.name);
    TextView num = convertView.findViewById (R.id.num);
    // Populate the data into the template view using the data object

    name.setText (user.name);
    num.setText (user.phone_number);


    return convertView;
}

}

列出项目XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="Number"
    android:textAppearance="?android:attr/textAppearanceMedium" />

2 个答案:

答案 0 :(得分:0)

试试这个,你的听众会工作:

android:descendantFocusability="blocksDescendants"

在列表中的第一个父布局上 答案积分:https://www.stackoverflow.com/a/13415566

希望这有帮助!

答案 1 :(得分:0)

如果您的项侦听器不起作用,则可以在xml布局中的ListView上应用它。

android:descendantFocusability="blocksDescendants" 

如果您需要适配器类(如

),则可以添加许多点击侦听器
   public class MyAdapter extends ArrayAdapter<Contact> {
        Context context;
        public MyAdapter(Context context, ArrayList<Contact> users) {
            super(context, 0, users);
            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            Contact user = getItem(position);

            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewadapter, parent, false);
            }
            TextView name = convertView.findViewById(R.id.name);
            TextView num = convertView.findViewById(R.id.num);
            // Populate the data into the template view using the data object

            name.setText(user.name);
            num.setText(user.phone_number);
            // can set click on name or num
            name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, position + "position clicked", Toast.LENGTH_SHORT).show();
                }
            });
            // can set click on full list item
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, position + "position clicked", Toast.LENGTH_SHORT).show();
                }
            });
            return convertView;
        }
    }