Below is the code for my first attempt at setting up a list fragment that clicks through to an activity (just as a test). I've followed the Android docs example at: List Fragment Example
However, onListItemClick shows as "never used". When I run the app my list shows up, but when I click on the items in it I just get the following returned:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
The XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.administrationuser.piclo.ListViewFragment"
tools:showIn="@layout/activity_list_view"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp"
>
<FrameLayout
android:id="@+id/list_container_id"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" >
</ListView>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
And here is the list fragment:
package com.example.administrationuser.piclo;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class ListViewFragment extends ListFragment {
public ListViewFragment() {}
String[] myStringArray = new String[]{"aa","bbb","ccccc","dddddd"};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, myStringArray));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
int mCurCheckPosition = index;
Intent intent = new Intent();
intent.setClass(getActivity(), MessageDetails.class);
intent.putExtra("Inty", mCurCheckPosition);
startActivity(intent);
startActivity(intent);
}
}
I have solved the problem. It is trivial and would seem obvious to any seasoned Android developer, but here it is for the beginners out there who may fall into this trap....
...do not name your parent activity 'ListView'... it confuses the OnListItemClick override cause it doesn't know which class of ListView it should be overriding... the actual one or the one that you may have unfortunately created through the worst possible choice of name for your parent activity.