【问题标题】:Android | How to set click listener in ListItem with ArrayAdapter安卓 |如何使用 ArrayAdapter 在 ListItem 中设置点击监听器
【发布时间】:2016-11-24 22:32:52
【问题描述】:

我的问题是如何使用ArrayAdapter 扩展从自定义ListView 中单击ListItem

我在这里找到了一个与我的问题类似的thread,但另一个使用 ListActivity 扩展,并且接受的答案使用了 AppCompatActivity 扩展中不可用的方法 getListView()

那么如何在我的 ListView 上设置一个点击监听器而不改变我的 Activity 类来扩展 ListActivity?

PatientListAdapter.class

public class PatientListAdapter extends ArrayAdapter<Patient> {
    public PatientListAdapter(Context context, ArrayList<Patient> patient) {
        super(context, 0, patient);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Patient patient = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext())
                    .inflate(R.layout.list_patients, parent, false);
        }

        ...

        return convertView;
    }
}

PatientList.class

public class PatientList extends AppCompatActivity {
    private ListView patientsListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_list);

        ...

        ArrayList<Patient> arrayOfUsers = new ArrayList<>();
        final PatientListAdapter adapter = new PatientListAdapter(this, arrayOfUsers);
        ListView listView = (ListView) findViewById(R.id.patients_listview);
        listView.setAdapter(adapter);

        ...
    }
}

我试过这段代码,但没有用

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        android.util.Log.d("ListView", "Clicked");
    }
});

list_patients.xml

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

    <LinearLayout
        android:id="@+id/info0"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/list_id"
            android:text="ID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <TextView
            android:id="@+id/list_casenumber"
            android:text="Case #"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <TextView
            android:id="@+id/list_datetime"
            android:text="DateTime Admitted"
            android:textAlignment="textEnd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4" />

    </LinearLayout>

    <LinearLayout
        android:layout_below="@+id/info0"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/info1">

        <TextView
            android:id="@+id/list_fullname"
            android:text="Name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <TextView
            android:id="@+id/list_sex"
            android:text="Sex"
            android:textAlignment="textEnd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:layout_below="@+id/info1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/info2">

        <TextView
            android:id="@+id/list_physician"
            android:text="Physician"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <TextView
            android:id="@+id/list_room"
            android:text="Room"
            android:textAlignment="textEnd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </LinearLayout>
</RelativeLayout>

content_patient_list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_patient_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.jessi.pms.PatientList"
    tools:showIn="@layout/activity_patient_list">

    <ListView
        android:id="@+id/patients_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

编辑:添加了另一个给出错误的布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.jessi.pms.PatientList">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_patient_list"
        android:id="@+id/include" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • onItemClickListener 有什么问题?
  • 我不确定这个问题的“双重性”。我在列表项上看不到任何可点击或可聚焦的视图。
  • @NickCardoso 我不知道,但在其他线程上,如果在 ListActivity 扩展上使用它似乎可以工作。
  • 您是否在折叠的任何代码中设置了任何侦听器?

标签: android android-arrayadapter


【解决方案1】:

我通过删除我在布局文件中添加的 ScrollView 解决了我的问题。我不知道 ListView 已经包含了 ScrollView。

将 ListView 放在 ScrollView 中确实不会调用 OnClickItemListener。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多