-1

アクティビティを拡張するメイン アクティビティがあります。Android サンプルのようなドロワー ナビゲーションを使用します。スライド メニューには、チャプターのリストがあります。クリックすると、その章のレッスンのリストがメイン アクティビティ内のコンテンツ フラグメントに表示されます。

したがって、メイン アクティビティには Fragment を拡張するクラスがあります。フラグメントのコンテンツを設定するには、次のようなメソッドがあります。

    public static class contentFragment extends Fragment {
    public static final String ARG_CATEGORY_NUMBER = "category_number";

    public contentFragment() {
        // Empty constructor required for fragment subclasses
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


            View rootView = inflater.inflate(R.layout.fragment_content, container, false);
            int i = getArguments().getInt(ARG_CATEGORY_NUMBER);
            String category = getResources().getStringArray(R.array.category)[i];
            ((MainActivity) this.getActivity()).refreshDisplay(this.getActivity(),rootView, category);
            getActivity().setTitle(category);
            return rootView;
        }
}

これは、listView にコスチューム アダプターを設定する refreshDisplay メソッドです。

public void refreshDisplay(Context context, View view, String category) {

    List<Lesson> lessonByCategory = datasource.findByCategory(category);
    ListView lv = (ListView) view.findViewById(R.id.listView);
    ArrayAdapter<Lesson> adapter = new LessonListAdapter(context, lessonByCategory);
    lv.setAdapter(adapter);
    }

さて、onListItemClick を使用して、クリックされた項目の詳細について新しいアクティビティを開始するにはどうすればよいですか? 私は以下の方法を持っています。でもどこに置いたらいいのかわからない。refreshDisplay()の呼び出し後に Fragment を挿入すると、クリックされたアイテム (チャプター) の位置が取得されます。これは、フラグメント (私のコンテンツ フラグメント) 内のリスト内のアイテム (レッスン) ではなく、スライド メニューです。 .

protected void onListItemClick(ListView l, View v, int position, long id) {
    Log.i(LOGTAG, "onListItemClick call shod");

    Lesson lesson = lessons.get(position);

    Intent intent = new Intent(this, LessonDetailActivity.class);

    intent.putExtra(".model.Lesson", lesson);
    intent.putExtra("isStared", isStared);

    startActivityForResult(intent, LESSON_DETAIL_ACTIVITY);

}

どうすればこれを修正できますか? 私が欲しいのは、章のリストを含むスライド メニューがあることです。章をクリックすると、その章に関連するレッスンが表示されます (これまでの動作)。そして、レッスンをクリックすると、そのレッスンの詳細に関する新しいアクティビティが開かれます (これは私の問題です)。

4

1 に答える 1

0

refreshDisplay()メソッドで、次のようにリスナをリスト ビューに追加します。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        Log.i(LOGTAG, "onListItemClick call shod");

        // get the Lesson object for the clicked row
        Lesson lesson = m_adapter.getItem(position);

        // use this if your `refreshDisplay()` method is in your activity
        Intent intent = new Intent(MainActivity.this, LessonDetailActivity.class);

        // use this if you `refreshDisplay()` method is in your fragment
        Intent intent = new Intent(getActivity(), LessonDetailActivity.class);

        intent.putExtra(".model.Lesson", lesson);
        intent.putExtra("isStared", isStared);

        startActivityForResult(intent, LESSON_DETAIL_ACTIVITY);
    }
}

onListItemClick()または、メソッドからコードを移動することもできますonItemClick()

編集:

Lessonクリックしたオブジェクトをアダプターから取得する方法の例を追加しました。これを機能させるには、アダプターをメンバー変数として宣言する必要があります。MainActivityをアクティビティの名前に置き換えます。

于 2013-09-20T21:14:02.707 に答える