我想通過它的位置顯示列表視圖中的項目。例如:如果我點擊第三位,我需要顯示第三項。這是我的代碼。我想在單擊項目按鈕時顯示。如何在Android中按位置顯示列表視圖中的項目
for (int k = 0; k < jsonarrays.length(); k++) {
JSONObject main_menus = jsonarrays.getJSONObject(k);
SUB_MENU_ID = main_menus.getString(TAG_SUB_MENU_ID);
SUB_MENU_NAME = main_menus.getString(TAG_SUB_MENU_NAME);
HashMap<String, String> submenu = new HashMap<String, String>();
submenu.put(TAG_SUB_MENU_ID, SUB_MENU_ID);
submenu.put(TAG_SUB_MENU_NAME, SUB_MENU_NAME);
submenus.add(submenu);
}
@Override
protected void onPostExecute(String result) {
submenuadapter = new SubmenufoodCategoryAdapter(submenus, getActivity());
sb_list.setAdapter(submenuadapter);
sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});}
這是我的適配器類別:
public class SubmenufoodCategoryAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> mainmenu;
HashMap<String, String> mainmenumap = new HashMap<String, String>();
Context con;
public SubmenufoodCategoryAdapter(ArrayList<HashMap<String, String>> submenu, Context con) {
super();
this.mainmenu = submenu;
this.con = con;
}
@Override
public int getCount() {
return mainmenu.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainmenumap = mainmenu.get(position);
View rowview;
rowview = layoutInflater.inflate(R.layout.submenu_button, null);
holder.btn = (Button) rowview.findViewById(R.id.submenu_layout_button);
holder.btn.setText(mainmenumap.get(FoodMenuItemFragment.TAG_SUB_MENU_NAME));
return rowview;
}
public class Holder {
Button btn;
}
}