OnItemClickListner 无法帮助我,我希望点击列表项每个项目转到下一个活动
java 类
public class Tab1 extends Fragment {
private ListAdapter mAdapter;
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private ListView listview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_layout_one, container, false);
prepareList();
mAdapter = new ListAdapter(getActivity(), listCountry, listFlag);
//Missed code to here
listview = (ListView)rootView.findViewById(R.id.card_listView);
listview.setAdapter(mAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
return rootView;
}
public void prepareList()
{
listCountry = new ArrayList<String>();
listCountry.add("India");
listCountry.add("USA");
listCountry.add("Canada");
listCountry.add("AUS");
listFlag = new ArrayList<Integer>();
listFlag.add(R.drawable.india);
listFlag.add(R.drawable.usa);
listFlag.add(R.drawable.canada);
listFlag.add(R.drawable.aus);
}
}
答案 0 :(得分:2)
小错误,请使用onItemClick
方法
在这里显示Toast消息。和新活动发布的演示代码
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// Add your staff here
Toast.makeText(getActivity(),"Listview Click"+position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), Your_Desise.class);
startActivity(intent);
}
});
试试这种方式。我相信它会对你有帮助。
答案 1 :(得分:0)
您必须调用AdapterView
onItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"Click" +position, Toast.LENGTH_SHORT).show();
}
});