0

我有一个问题,当我设置特定的适配器时,试图让我的android应用程序跟踪用户输入。Android ListFragment适配器不允许用户点击

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

@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没有的地方选择?

+0

仍然在与此挣扎。我想这是一个问题,我想如何使用点击监听器。我已经覆盖片段类中的onListItemClick,但我不确定是否由于这是自定义适配器,我需要在适配器中创建一个新的适配器? – massphoenix

+0

因此,我在适配器中设置了一个侦听器并注册了点击。我想接下来要做的事情是编写一些回调,将点击从适配器中推回主要活动。如果这是一个正确的方法来完成它,或者这应该全部在片段中设置或者如果没有真正的区别,我想要输入。 – massphoenix

回答

0

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

相关问题