【问题标题】:android-How to refresh data in listview with Baseapdaterandroid-如何使用Baseapdater刷新listview中的数据
【发布时间】:2014-09-16 12:35:43
【问题描述】:

这是我的代码。我得到新数据,我需要刷新列表。我正在使用基本适配器来制作我的 listview 。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv=(ListView) findViewById(R.id.listView1);
    lv.setAdapter(new myAdapter(this));

}

class SingleRow{
    String title; 
    String description;
    int image ; 
    SingleRow(String title,String description, int image) {
        this.title=title; 
        this.description=description;
        this.image=image;
    }
}

class myAdapter extends BaseAdapter{
    ArrayList<SingleRow> list; 
    Context context;
    myAdapter(Context c){
        context=c; 
        list=new ArrayList<SingleRow>();
        Resources res=c.getResources();
        String[] titles=res.getStringArray(R.array.titiles);
        String[] descriptions=res.getStringArray(R.array.descriptions);
        int[] images={R.drawable.a1 ,R.drawable.a2,R.drawable.a3,R.drawable.a49};
        for(int i =0; i<4;i++){  
            list.add(new SingleRow(titles[i], descriptions[i], images[i]));
        }
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row= inflater.inflate(R.layout.custom_list_items, parent,false);

        TextView title= (TextView) row.findViewById(R.id.title);
        TextView description= (TextView) row.findViewById(R.id.desc);
        ImageView image=(ImageView) row.findViewById(R.id.imageView1);

        SingleRow temp= list.get(position);

        title.setText(temp.title);
        description.setText(temp.description);
        image.setImageResource(temp.image);
        return row;
    }

我应该在哪里放置 notifyDataSetChanged() 或其他方法来刷新我的列表并显示新数据?

谢谢

【问题讨论】:

标签: android android-listview


【解决方案1】:

NotifyDataSetChanged 用于通知与适配器关联的数据集。现在理想情况下,当您设置适配器之后,您应该调用 notifydatasetChanged 以使先前的数据集无效并从适配器加载新数据集。

也不建议每次都创建一个新的适配器对象。

你可以这样做:->

Myadapter adapter = new Myadapter(context,dataset of your custom arraylist);//Constructor

之后,每当您获得新数据集时,请调用此

adapter.ArrayList<T> = new ArrayList<T>;

然后

adapter.notifyDataSetChanged();

如果您不想更改任何内容,请在新适配器调用后在代码中更改...调用 适配器.notifyDataSetChanged();

编辑:

在活动中:

ArrayList<SingleRow> rows = new ArrayList<SingleRow>();
myAdapter theAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv=(ListView) findViewById(R.id.listView1);
    Resources res = getResources();
    String[] titles=res.getStringArray(R.array.titiles);
    String[] descriptions=res.getStringArray(R.array.descriptions);
    int[] images={R.drawable.a1 ,R.drawable.a2,R.drawable.a3,R.drawable.a49};
    for(int i =0; i<4;i++){  
        rows.add(new SingleRow(titles[i], descriptions[i], images[i]));
    }
    theAdapter = new myAdapter(this, rows);
    lv.setAdapter(theAdapter);
}

void doAdd()
{
    rows.add(new new SingleRow(...some values...);
    theAdapter.notifyDataSetChanged()
}

void doClear()
{
    rows.clear();
    theAdapter.notifyDataSetChanged()
}

在适配器中:

myAdapter(Context c, ArrayList<SingleRow> rows)
{
    context=c;
    list = rows;
}   

你也可以看看ArrayAdapter的实现...

【讨论】:

  • Now Ideally when ever you are setting an adapter after that you should call notifydatasetChanged bu..sh...
  • 那么理想情况下你应该在哪里调用 selvin ?
  • Adapter.notifyDataSetChanged 通知父级AdapterView 刷新视图...与AdapterView.setAdapter 相同...无需在AdapterView.setAdapter 之后调用Adapter.notifyDataSetChanged ...
  • 现在如果我不想为那个适配器创建一个新对象。那我该怎么办?
  • 所以请改写你的分析器...也尝试使用 fx ArrayList 'custom arraylist' 让代码看起来更像代码...
【解决方案2】:

任何时候你调用 list.add (something) 你应该调用 notifyDtaSetChnged(); 如果发生在 BaseAdpter 类之外,使用 adapter.NotifyDataSetChnged()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多