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

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

问题描述

我有一个数组列表,我想更新特定项目.我正在使用此行向列表添加数据:

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

这是将数据添加到 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.

我正在用这一行更新数据:

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.:我正在使用这个数组列表和一个适配器

推荐答案

正如@itachiuchiha 提到的,你应该使用 Map.您提到的自定义数字"是您的 (整数),而值是 Random 对象.

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

顺便说一句,为了回应您的评论,下面是一个使用 Map 作为底层数据源的 Android Adapter 示例.

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(Map randoms) 方法.

虽然您可以在适配器中公开一个方法来更新映射中的单个条目,但适配器不应该负责处理对映射的修改.我更喜欢再次传递整个地图——它仍然可以是对同一个对象的引用,但适配器不知道或不关心;它只知道:我的底层数据源已更改/修改,我需要告诉我的观察者他们应该通过调用 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()".

或者,您可以在修改底层 Map 时在外部调用适配器上的 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天全站免登陆