0

My question is how do I click a ListItem from a custom ListView using an ArrayAdapter extensions?

I have found a thread here that is similar with my question but the other uses a ListActivity extensions and the accepted answer uses the method getListView() which is not available in AppCompatActivity extensions.

So how do I set a click listener on my ListView without changing my Activity class to extend 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);

        ...
    }
}

I have tried this code but to no avail

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>

EDIT: Added the other layout file that gives the error

<?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>
Community
  • 1
  • 1
Jess
  • 29
  • 7

1 Answers1

0

I fixed my problem by removing ScrollView that i have added on my layout file. I didn't know that ListView already includes ScrollView.

Putting a ListView inside a ScrollView does make OnClickItemListener to not be invoked.

Jess
  • 29
  • 7