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

时间:2016-03-03 08:05:23

标签: android listview android-json

我希望通过它的位置显示Listview中的项目。例如:如果我点击第3个位置,我需要显示第3个项目。这是我的代码。我想在单击项目按钮时显示。

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

4 个答案:

答案 0 :(得分:1)

请勿将此代码与您的要求进行比较。我只是给您一个想法..

我认为你正试图将所有数据从适配器扩展到List-view ..我做了一个示例,我在对话框中显示从适配器到List-view的所有细节,所以

    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();

在这里你可以看到。将数据导入适配器后,您可以调用该字段的 ArrayList.get(position).value ..点击它将根据位置获取数据

答案 1 :(得分:0)

您可以启动另一个Activity并使用Intent

发送这些数据

答案 2 :(得分:0)

您可以尝试使用以下代码段:

sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //save data form here to some variables and on button click send data to where you want to show whether in new activity or something else

             sbmenus.get(2).get(FoodMenuItemFragment.TAG_SUB_MENU_NAME);

                 }
            });}

答案 3 :(得分:0)

您可以使用AlertDialog。 Here就是一个例子,回答2。

public void dialogBox(String message) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage(message);
    alertDialogBuilder.setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
        }
    });

    alertDialogBuilder.setNegativeButton("cancel",
        new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {

        }
    });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

使用要显示的数据调用itemClick()上的dialogBox(String)方法。