Search code examples
androidonclickandroid-listfragmentandroid-cursoradapterselectable

Android ListFragment Adapter not allowing user clicks


I am having a problem trying to get my android app to track user input when when I set a specific adapter.

So in my fragment class I have this function that is picked up when the fragment is picked up into the activity.

@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
    System.out.println("clicked");
    // Indicates the selected item has been checked
    getListView().setItemChecked(pos, true);

    // Inform the QuoteViewerActivity that the item in position pos has been selected
    mListener.onListSelection(pos);
}


@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()");
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()");
    return super.onCreateView(inflater, container, savedInstanceState);
}

public void onActivityCreated(Bundle savedState) {
    Log.i(TAG, getClass().getSimpleName() + ":entered onActivityCreated()");
    super.onActivityCreated(savedState);


    dbHelper= new dbHelper(getActivity());
    Cursor cursor = dbHelper.selectJoinDecksQuestions("jjj");
    cursor.moveToFirst();
    setListAdapter(new QuestionCursorAdapter(getActivity(), cursor));

}

which calls the adapter and populates the screen with the data returned by the cursor into a list of text views. The problem is that none of this data is selectable.

If I change the onActivityCreated method to look like this data is displayed and is selectable:

public void onActivityCreated(Bundle savedState) {
    Log.i(TAG, getClass().getSimpleName() + ":entered onActivityCreated()");
    super.onActivityCreated(savedState);

     Set the list adapter for the ListView
     Discussed in more detail in the user interface classes lesson
     setListAdapter(new ArrayAdapter<String>(getActivity(),
           R.layout.question_fragment, MainContentActivity.mTitleArray));
}

The problem adapter looks like this

public QuestionCursorAdapter(Context context, Cursor c) {
    // that constructor should be used with loaders.
    super(context, c, 0);
    inflater = LayoutInflater.from(context);
    System.out.println("leaving constructor of the questioncursoradapter");
    System.out.println(getCount());
}


@Override
public void bindView(View view, Context context, Cursor cursor) {
    System.out.println("in bind view");
    TextView list_item = (TextView)view.findViewById(R.id.question_in_list);
    list_item.setText(cursor.getString(cursor.getColumnIndex(COLUMN_NAME_QUESTIONTEXT)));
    list_item.setMovementMethod(new ScrollingMovementMethod());
    int position = cursor.getPosition(); // that should be the same position

    System.out.println("leaving bind view");*/
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    System.out.println("newView in QuestionCursorAdapter entered");
    //question_fragment/cusoradaptertest
    View v = inflater.inflate(R.layout.question_fragment, null);
    System.out.println("leave newview");
    return v;

}

Any thoughts on why the ArrayAdapter returns data that is selectable where my custom CursorAdapter does not ?


Solution

  • So I set up the clicklistener in the adapter and everything worked fine! The only other thing I had to do was allow the fragment to pass it's activity to the adapter so the main activity could be called back from there. I'm hoping this isn't inferior performance wise but it seems to get the job done so I'll set the answer here in case anyone else comes across this.