【问题标题】:Android ListFragment Adapter not allowing user clicksAndroid ListFragment 适配器不允许用户点击
【发布时间】:2015-10-27 01:59:55
【问题描述】:

我在设置特定适配器时试图让我的 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 没有的任何想法?

【问题讨论】:

  • 仍在为此苦苦挣扎。我认为这是我如何尝试使用点击监听器的问题。我已经在片段类中覆盖了 onListItemClick,但我不确定是否因为这是一个自定义适配器,我需要在适配器中创建一个新的?
  • 所以我在适配器中设置了一个监听器,它记录了点击。我想我接下来要做的就是编写一些回调,将点击从适配器推回主要活动。如果这是一种正确的方法,或者应该全部在片段中设置,或者没有真正的区别,我想要输入。

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


【解决方案1】:

所以我在适配器中设置了 clicklistener,一切正常!我唯一要做的另一件事是允许片段将其活动传递给适配器,以便可以从那里回调主要活动。我希望这不是表现不佳,但它似乎可以完成工作,所以我会在这里设置答案,以防其他人遇到这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2023-03-16
    相关资源
    最近更新 更多