简体   繁体   中英

How to store a Hashmap value in a variable on LongclickListener in listview?

I am new to android development.
I want that when the user long presses the item from the list its value gets stored in a variable so that I can use that variable further for running my other function.

public class Second extends Fragment {

    public Second() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ListView misc=(ListView) getActivity().findViewById(R.id.lv_misc);

        HashMap<String,String> itemmisc = new HashMap<>();
        itemmisc.put("Aloo Kathi Roll","Calories: 460");
        itemmisc.put("Paneer Roll","Calories: 197");
        itemmisc.put("Egg Roll","Calories: 196");
        itemmisc.put("Paneer Egg Roll","Calories: 650");
        itemmisc.put("Makhani Roll","Calories: 420");
        itemmisc.put("Double Egg Roll","Calories: 315");
        itemmisc.put("Chocolate Donut","Calories: 340");
        itemmisc.put("Chole Bhature","Calories: 427");
        itemmisc.put("Aloo Parantha","Calories: 177");
        itemmisc.put("Paneer Parantha","Calories: 234");
        itemmisc.put("Plain Maggi","Calories: 188");
        itemmisc.put("Cheese Maggi","Calories: 322");
        itemmisc.put("Hot Chocolate","Calories: 77");
        itemmisc.put("Vada Pav","Calories: 197");
        itemmisc.put("Veg Hot Dog","Calories: 143");
        itemmisc.put("Cheese Fries","Calories: 560");
        itemmisc.put("Mayo Fries","Calories: 507");
        itemmisc.put("White Pasta","Calories: 200");
        itemmisc.put("Red Pasta","Calories: 250");
        itemmisc.put("Strawberry Shake","Calories: 282");
        itemmisc.put("Chocolate Shake","Calories: 590");
        itemmisc.put("Aloo Burger","Calories: 367");
        itemmisc.put("Cheese Burger","Calories: 303");

        List<HashMap<String,String>> listItemmisc = new ArrayList<>();
        SimpleAdapter adapter3 = new SimpleAdapter(getActivity(),listItemmisc,R.layout.list_items_misc, new String[]{"First Line","Second Line"},
                                                   new int[]{R.id.text1,R.id.text2});
        Iterator it = itemmisc.entrySet().iterator();
        while(it.hasNext()){
            HashMap<String,String> resultMap = new HashMap<>();
            Map.Entry pair = (Map.Entry) it.next();
            resultMap.put("First Line",pair.getKey().toString());a
            resultMap.put("Second Line",pair.getValue().toString());
            listItemmisc.add(resultMap);
        }
             misc.setAdapter(adapter3);

    }
}

This is the java file where I have created a list view using hashmap for mainline and subline. From this list, I want to retrieve the value of the long pressed item. The longpressclicklistener uses position but I don't know how to receive the position from this hashmap and store only the mainline in it.

1- Use a Custom Adapter extends BaseAdapter (for example xxx) 1-1 In your adapter the Map objet must be accessible(for example by sending to custom adapter) 2- In your custom adapter rewrite override method of getView() 3- In getView the position is in your access.By getting the element of your listView and with help of the 'position' parameter, you could get the value item of the selected list Item. Then compare the value selected with key(s) of your Map. 4- When a Map key Item is equal with the extracted selected value item(from list),get the value of the adapted key from the Map object.In this time you has behind value of selected list item and could do your task. All of these tasks are done in getView method of your custom adapter.

custom adapter constractor sample:

public SimpleIndividualAdapter(Context iApplicationContext, Collection<SimpleIndividual> iData ,param2,param3,... ){} 

In above for your need the iData maybe a Map Object of HashMap type. For more investigation refer to creating a custom adapter.

In getView method(override method), you have view and you could get the list and get item selected by position parameter.

Part of getView()... :

@Override
public View getView(final int position, View convertView, ViewGroup parent){}

This answer shows how to set the OnItemLongClickListener . Now to get the item you can simply query your list of items:

HashMap<String, String> item = listItemmisc.get(position);

or your adapter:

HashMap<String, String> item = adapter3.getItem(position);

Now I don't know your use case, but I'm not sure you want a list of HashMap<String, String> . You might want to create a class Food and give the adapter your list of Food instead. This is a good read on Java and Object-Oriented Programming Concepts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM