我正在上课,如下所示
// All necessary imports are here
public class More extends Activity {
String[] MoreItems = { "Transfers", "Budgets", "Branches", "Tools", "News",
"Customer Service", "Settings", "Help", "About" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.more_view);
ListView moreListView = (ListView) findViewById(R.id.moreListView);
MoreListAdapter listAdapter = new MoreListAdapter();
moreListView.setAdapter(listAdapter);
// accountsTypeListView.setOnItemClickListener(listClickListner);
}
class MoreListAdapter extends ArrayAdapter<String> {
MoreListAdapter() {
super(More.this, R.layout.list_item, MoreItems);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.list_item, parent, false);
} else {
row = convertView;
}
TextView tv = (TextView) row.findViewById(R.id.textItem);
tv.setText(getItem(position));
return row;
}
}
}
它会生成List,我想在各自的点击上调用相应的活动,如果用户点击转移则会显示转移活动, 如何在此列表中调用onClickListener以及如何在单击时启动Activity。
答案 0 :(得分:51)
你也可以这样做..
moreListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("############","Items " + MoreItems[arg2] );
}
});
答案 1 :(得分:48)
有两个选项可以处理每一行的点击事件。
1)如果您的班级延伸ListActivity
,您可以覆盖以下方法。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//do something here using the position in the array
}
2)在getView()
方法
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
答案 2 :(得分:11)
例如:
ListView lv = getListView();
lv.setAdapter(listAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent i = new Intent(More.this, NextActvity.class);
//If you wanna send any data to nextActicity.class you can use
i.putExtra(String key, value.get(position));
startActivity(i);
}
});
答案 3 :(得分:1)
你的“更多”类必须扩展ListActivity而不是Activity,然后你可以覆盖onListItemClick
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//do something here using the position in the arrya
}
编辑:忘了说,在你的布局中你必须调用ListView:android:id =“@ android:id / list”