【发布时间】:2018-07-30 18:56:04
【问题描述】:
我有一个包含课程列表的片段:
我希望在单击第一个选项时打开一个新视图。 我该怎么做?
下面是图片中片段的代码
public class FirstYearFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.word_list, container, false);
// Create a list of words
final ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word(R.string.firstYearDirection, R.string.algevra, R.drawable.algevra));
words.add(new Word(R.string.firstYearDirection, R.string.arxaia, R.drawable.arxaia));
words.add(new Word(R.string.firstYearDirection, R.string.ekuesi, R.drawable.ekthesi));
words.add(new Word(R.string.firstYearDirection, R.string.geometria, R.drawable.geometry));
words.add(new Word(R.string.firstYearDirection, R.string.fysiki, R.drawable.fusiki));
words.add(new Word(R.string.firstYearDirection, R.string.xhmeia, R.drawable.xhmeia));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_first_year);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) rootView.findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
return rootView;
}
}
【问题讨论】:
-
我使用下面但我得到错误 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> adapterView, View view, int position, long l) { Intent 意图 = new Intent(getActivity(), FirstYearForumView.class); startActivity(intent); } });