Android ListFragment Adapter不允许用户点击

时间:2015-10-27 01:59:56

标签: android onclick android-listfragment android-cursoradapter selectable

我在设置特定适配器时试图让我的Android应用程序跟踪用户输入时遇到问题。

所以在我的片段类中,我有这个函数,当片段被拾取到活动中时会被拾取。

@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));

}

调用适配器并使用光标返回的数据将屏幕填充到文本视图列表中。问题是这些数据都不是可选的。

如果我将onActivityCreated方法更改为显示此数据并且可以选择:

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));
}

问题适配器看起来像这样

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;

}

有关为什么ArrayAdapter返回我的自定义CursorAdapter所不能选择的数据的想法?

1 个答案:

答案 0 :(得分:0)

所以我在适配器中设置了clicklistener,一切正常!我唯一需要做的就是让片段将它的活动传递给适配器,这样就可以从那里调用主活动。我希望这不是性能低下的,但它似乎完成了工作,所以我会在这里设置答案,万一其他人遇到这个。