如何让用户单击列表菜单中的选定项目,选择后将意图转到另一个页面。我知道这个问题在stackoverflow中被问了很多,但我已经经历了大部分,我仍然迷路。希望我能在这里得到一些帮助。
示例:当用户在列表中选择“a”时,它将转到另一个活动我如何onclick listener
在此代码中实现?
编辑后,我的意图以另一种方式不起作用。
我在我的主程序上添加了代码,但是当我运行程序时,我仍然无法点击
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// write your start another activity code here
switch(i){
case 0:{
Intent intent1 = new Intent(LeftAndRightActivity.this, DisplayActivity.class);
startActivity(intent1);
}
}
}
});
rightlistfragment.xml
public class RightListFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.rightlist, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem("a"));
adapter.add(new SampleItem("b"));
adapter.add(new SampleItem("c"));
adapter.add(new SampleItem("d"));
adapter.add(new SampleItem("e"));
adapter.add(new SampleItem("f"));
setListAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// write your start another activity code here
switch(i){
case 0:{
startActivity(new Intent(RightListFragment.this,FragmentBindAcc.class));
}
}
}
TextView text1 = (TextView) getView().findViewById(R.id.loginName);
text1.setText("TEST");
}
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag) {
this.tag = tag;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.rightrow, null);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.arrow);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(getItem(position).tag);
return convertView;
}
}
}
谢谢