Android |如何使用ArrayAdapter在ListItem中设置单击侦听器

时间:2016-11-24 12:16:49

标签: android android-arrayadapter

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

我在这里发现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>

1 个答案:

答案 0 :(得分:0)

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

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