嘿,我已经找了几个小时,试图找到一个解决方案,我的目标是在打开另一个活动时打开Listview.实际上,我得到它可以打开另一个活动,当它的点击,但如何得到它,使每个列表项目将打开自己的活动?我非常抱歉如果这个问题已经回答了,但我发现的链接并没有真正描述代码正在做什么[是的,我是新手:)]
这是代码im使用
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] countries = getResources().getStringArray(R.array.countries_array); setListAdapter(new ArrayAdapter<String>(this,R.layout.newfile,countries)); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent,View view,int position,long id) { // When clicked,show a toast with the TextView text Intent myIntent = new Intent(view.getContext(),Html_file.class); startActivityForResult(myIntent,0); } }); } }
解决方法
lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent,long id) { // When clicked,show a toast with the TextView text if(position == 1) { //code specific to first list item Intent myIntent = new Intent(view.getContext(),Html_file1.class); startActivityForResult(myIntent,0); } if(position == 2) { //code specific to 2nd list item Intent myIntent = new Intent(view.getContext(),Html_file2.class); startActivityForResult(myIntent,0); } } });