Arraylist custom location

蓝咒 提交于 2020-01-05 08:51:54

问题


I have an arraylist and I want to update specific item.I am adding data to list with this line:

randomsList.add(new Random(result.get(i).getAsJsonObject(),0));

This is adding datas to 0,1,2,3,4... locations so when I try to update an item I don't know which object is where.

I am updating data with this line:

randomsList.set(position,new Random(user,1));

I think if I use the custom numbers for location I can update specific item.My prototype:

randomsList.add({USER_ID},new Random(result.get(i).getAsJsonObject(),0));

And if I want to update it then I use this line:

randomsList.set({USER_ID},new Random(user,1));

Is this a good approach ? If your answer is no,how should be ?

P.S. : I am using this arraylist with an adapter


回答1:


As @itachiuchiha mentions, you should use a Map. The "custom numbers" you mention are your key (integers), and the value is the Random object.

As an aside, in response to your comment, below is an example of an Android Adapter that uses a Map as the underlying datasource.

public class RandomsAdapter extends BaseAdapter {

    private Map<Integer, Random> randoms;

    public RandomsAdapter(Map<Integer, Random> randoms) {
        this.randoms = randoms;
    }

    public void updateRandoms(Map<Integer, Random> randoms) {
        this.randoms = randoms;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return randoms.size();
    }

    @Override
    public Random getItem(int position) {
        return randoms.get(position);
    }

    @Override
    public long getItemId(int position) {
        // we won't support stable IDs for this example
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = createNewView();
        }
        update(view, songs.get(position));
        return view;
    }

    private View createNewView() {
        // TODO: create a new instance of your view and return it
        return null;
    }

    private void update(View view, Random random) {
        // TODO: update the rest of the view
    }

}

Note the updateRandoms(Map<Integer, Random> randoms) method.

While you could expose a method in the adapter to update a single entry in the Map, it shouldn't be the responsibility of the Adapter to handle modifications to the map. I prefer passing the entire map again - it could still be a reference to the same object, but the Adapter doesn't know or care; it just knows: "my underlying datasource has been changed/modified, I need to tell my observers that they should refresh their views by calling notifyDataSetChanged()".

Alternatively, you could call notifyDataSetChanged() on the adapter externally when you modify the underlying Map (this tells the ListView that its data is out of date and to request its views from the adapter again).



来源:https://stackoverflow.com/questions/25715154/arraylist-custom-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!