Android的列表与onclicklistener使用查看SampleAdapter [英] Android List with onclicklistener using SampleAdapter
问题描述
如何让用户点击列表中的菜单中选择的项目,选择后它会意图另一页。我知道这个问题已经被问了很多的计算器,但我已经通过大部分消失了,我仍然输了。希望我能有一些帮助在这里。
How to let users click on the selected item in the list menu and after select it will intent to another page. I know this question have been asked a lot in stackoverflow but i have gone through most of it and i am still lose. Hope i can have some help here.
示例::当用户选择是在列表中它会去另一个活动如何实施的onclick监听器
在此code吗?
Example: When the user select "a" in the list it will go to another activity how do i implement onclick listener
in this code?
编辑后,我的意图不会用另一种方法。结果
我增加了code对我的主,但是当我运行程序我仍然无法点击
After editing, my intent doesn't work in another way.
I added the code on my main but when i run the program i still can't click
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;
}
}
}
感谢
推荐答案
您可以通过 setOnItemClickListener
的的ListView得到这个
如下:
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
switch(arg2){
case 0:{
startActivity(new Intent(getApplicationContext(),ActivityA.class));
}
break;
case 1:{
startActivity(new Intent(getApplicationContext(),ActivityB.class));
}
break;
case 2:{
ExampleQuestion.topicNo=3;
startActivity(new Intent(getApplicationContext(),ActivityC.class));
}
break;
}
您可以把它作为列表视图
包含A,B和C.当用户选择一个,它为0的情况和对B案例2和C外壳3。
You can think of it as the listview
contains A, B and C. When user selects A, it is the Case 0 and for B Case 2 and for C case 3.
这篇关于Android的列表与onclicklistener使用查看SampleAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!