如何显示项目从 Listview 中的位置Android

标签: listview Android
发布时间: 2016/10/10 21:00:00
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要显示的项目从 Listview 由它是位置。例如︰ 如果我点击我需要显示第 3 项的第三位。这里是我的代码。我想要显示时该项目 button 单击。

 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) {

         }
      });}

这是我 Adapter 类.:

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;
    }
}

解决方法 1:

不要比较此代码与您的要求。 我只给你的想法。

我认为你试图夸大所有数据从 adapter 到列表视图。我制做了样品,在那里我只在展示所有的细节,从 adapter 到列表视图内 Dialog 所以

    builder.setAdapter(notes_second_adapter,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int position) {

                    notes_child_category_second.setText(notes_second_list
                            .get(position).get(KIDZDAY_TAG_DESCRIPTION));
                    acti_id_to_save = notes_second_list.get(position).get(
                            KIDZDAY_TAG_REF_ID);
                    //sampel_1 = "Nihal Srivastava";

                }
            });

    dialog = builder.create();
    dialog.show();

在这里你可以看到。后获取数据到 adapter 你可以调用ArrayList.get (位置).value 的这一领域。由您的点击,它会获取位置数据

赞助商