在程序中我寫了以下的類,所有需要的 imports 都在程序中。
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,我想在respective click上調用respective activities,比如用戶點擊Transfer ,它就會轉到 transfer Activity。如何在List上調用onClickListener,並且在click上開啟 Activity?
如果是要實現點擊相對應的Item進入相應的Activity的話,樓主可以這樣寫:
List> p = GlobalApplication.permission;
下面有幾個Activity你就寫幾個map放到list裡面
Map map = new HashMap();
map.put("app", NewsActivity.class);
p.add(map);
下面是ListView的監聽:
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (list.get(arg2).get("app") != null) {
Intent intent = new Intent(HomeActivity.this,
(Class<?>) list.get(arg2).get("app"));
startActivity(intent);
}
}
});
你可以試一下!