问题描述
所以,我有两个片段链接到布局,显示列表视图文件。该列表视图在XML定义,并具有一个字符串数组项。我想点击在ListView项目和开放的新的活动。有在另一个ListView和9 8项。在onItemClick code,我怎么创造意图,开始根据项目活动的点击?我将创建每件1类作为其自己的活动。如何通过这个code?
的onItemClick方法里面意图开始在班级活动 类CommunityFragment扩展片段{ @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.community_fragment,集装箱,FALSE);
LV的ListView =(ListView控件)view.findViewById(R.id.communityListView);
lv.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
// TODO自动生成方法存根 }
});
返回视图。
}
}
类ResourcesFragment扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.resources_fragment,集装箱,FALSE);
LV的ListView =(ListView控件)view.findViewById(R.id.resourcesListView); lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
// TODO自动生成方法存根 }
});
返回视图。
}
}
请switch语句每个项目点击并打开活动如下相应:
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
// TODO自动生成方法存根
INT ITM = arg0.getItemAtPosition(ARG2);
开关(ITM){
情况下0:
Toast.makeText(m_context,位置零,Toast.LENGTH_SHORT).show();
意向意图=新意图(getActivity(),FirstActivity.class);
startActivity(意向);
打破;
情况1:
意图intent1 =新意图(getActivity(),SecondActivity.class);
startActivity(intent1);
打破;
案例2:
// .............................. }
});
So I have two fragments linking to layout files which display ListViews. The ListViews are defined in the xml and have entries from a string array. I want to click on items in the ListView and open new activities. There are 8 items in one ListView and 9 in the other. In the onItemClick code, how do I create intents to start activities based on the item clicked? I will create 1 class per item as its own activity. How can I start the activities in the classes via intents inside the onItemClick methods of this code?
class CommunityFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.community_fragment, container, false);
ListView lv = (ListView) view.findViewById(R.id.communityListView);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
// TODO Auto-generated method stub
}
});
return view;
}
}
class ResourcesFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.resources_fragment, container, false);
ListView lv = (ListView) view.findViewById(R.id.resourcesListView);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
return view;
}
}
Make switch statement for each items click and open activities accordingly as below:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
int itm=arg0.getItemAtPosition(arg2);
switch (itm) {
case 0:
Toast.makeText(m_context, "Position Zero", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), FirstActivity.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(getActivity(), SecondActivity.class);
startActivity(intent1);
break;
case 2:
//..............................
}
});
这篇关于从onItemClick开始在片段的ListView活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!