这里有listview进入选项卡片段。现在我想根据列表项开始新的活动。如何为以下代码实现intent活动。
public class TabFragment1 extends Fragment {
public TabFragment1(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tab_fragment1, container, false);
String[] menuItems = {"list1","list2","list3"};
ListView listView =(ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.activity_list_item,
menuItems
);
listView.setAdapter(listViewAdapter);
return view;
}
}
答案 0 :(得分:2)
您可以将setOnItemClickListener设置为listview并根据列表位置调用intent:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(position == 0){
Intent intent = new Intent(getActivity(), Activity1.class);
startActivity(intent);
}
else if(position == 1){
Intent intent = new Intent(getActivity(), Activity2.class);
startActivity(intent);
}
//Do as above for rest of the list items
}
});
要解决您的问题,请使用:
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.activity_list_item,
R.id.Your_textView_id_in_activity_list_item
menuItems
);
希望它有所帮助。