我正在使用此处的源代码:https://www.youtube.com/watch?v=rs9mSnWDlZo
基本上,他制作了一个适用于SherlockActionBar的列表适配器。他没有在代码中包含onListItemClick方法,我想用它来使每个列表片段开始一个新的意图,但是我自己实现了
我现在已经开始工作了,但我的问题是 - 我是否以正确的方式使用onListItemClick?
这是我如何使用它:
public class RandomList extends SherlockListFragment{
String[] list_contents = {
"item 1",
"item 2",
"item 3",
"item 4",
"item 5"
};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list_contents));
}
public void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Object obj = this.getListAdapter().getItem(position);
String pos = obj.toString();
if(pos.equals("item 1"))
{
Log.i("ListActivity", "found: " + pos);
//Start an intent....
}
else if(pos.equals("item 2"))
{
Log.i("ListActivity", "found: " + pos);
//Start an intent....
}
else if(pos.equals("item 3"))
{
Log.i("ListActivity", "found: " + pos);
//Start an intent....
}
else if(pos.equals("item 4"))
{
Log.i("ListActivity", "found: " + pos);
//Start an intent....
}
else if(pos.equals("item 5"))
{
Log.i("ListActivity", "found: " + pos);
//Start an intent....
}
}
}
答案 0 :(得分:1)
好的......真的很奇怪但是......创建一个像
这样的类数组Class[] intentClasses = {1.class,2.class,3.class,4.class,5.class};
然后在 onListItemClick
Intent intent = new Intent(getActivity(), intentClasses[position]);
startActivity(intent);