ArrayList的自定义位置 [英] Arraylist custom location

查看:107
本文介绍了ArrayList的自定义位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ArrayList,我想更新特定item.I正在将数据添加这一行列出:

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

这是加入到DATAS 0,1,2,3,4 ......位置,这样,当我尝试更新,我不知道哪一个对象,其中一个项目。

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.

我与这一行更新数据:

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

我想如果我使用的位置自定义的数字,我可以更新特定item.My原型:

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 我使用此ArrayList与适配器

推荐答案

由于@itachiuchiha提到,你应该使用地图。 自定义数字你提到是你的的(整数),和值是随机对象。

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

顺便说一句,在回答您的评论,下面是一个Android 适配器使用一个Map作为底层数据源的一个例子。

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
    }

}

请注意在 updateRandoms。(地图&LT;整数,随机&GT;偶合)

虽然可以在适配器来更新地图中的单个条目暴露的方法,它不应该是适配器来处理修改到地图的责任。我preFER再次通过整个地图 - 它仍然是对同一个对象的引用,但适配器不知道或不关心;它只是知道:我的基本数据源已经被改变/修改,我需要通过调用 notifyDataSetChanged()来告诉我的观察,他们应该刷新了自己的看法

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()".

另外,你可以调用 notifyDataSetChanged()适配器上的外部,当你修改底层的地图(这告诉它的数据是过时的ListView和要求其从适配器观点再次)。

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

这篇关于ArrayList的自定义位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆