0

I want to display the item from a Listview by it's position. For Example: If i clicked on 3rd position i need to display the 3rd item. Here is my code. I want to display when the Item button is clicked.

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

         }
      });}

And this is My Adapter Class.:

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;
    }
}
USER9561
  • 1,084
  • 3
  • 15
  • 41
Dev Tamil
  • 629
  • 1
  • 9
  • 25

4 Answers4

1

Don’t compare this code with your requirement.. I am just giving you Idea..

I think You are trying to inflating all data from adapter to List-view .. I made a sample where I am displaying all details from adapter to List-view inside a Dialog so

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

Here You can see. after getting data into adapter you can call ArrayList.get(position).value of that field.. By your click it will fetch data according to position

kitkat
  • 142
  • 10
0

you can start another Activity and send these data with the Intent

inkedTechie
  • 684
  • 5
  • 13
0

You can give try to the following code snippet:

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

                 }
            });}
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

You can use a AlertDialog. Here is an example, answer 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();
}

Call the dialogBox(String) method on your itemClick(), with the data you want to display.

Community
  • 1
  • 1