自定义适配器列表视图未刷新

时间:2013-05-02 06:18:41

标签: android android-listview custom-adapter

这是我的适配器类。

 public class CustomListViewAdapterRetailList extends ArrayAdapter<RowItem> {


Context context;
public ImageLoader imageLoader;


public CustomListViewAdapterRetailList(Context context,
        int textViewResourceId, List<RowItem> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    imageLoader = new ImageLoader(context);
}


/* private view holder class */
private class ViewHolder {
    ImageView retailerImage;
    TextView retailerName;
    TextView retailerSlogan;
    TextView msgDesc;
    ImageView typeImage;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.retailer_list_list_item,
                null);
        holder = new ViewHolder();
        holder.retailerImage = (ImageView) convertView
                .findViewById(R.id.retailer_list_item_iv_icon);
        holder.retailerName = (TextView) convertView
                .findViewById(R.id.retailer_list_item_tv_title);
        holder.retailerSlogan = (TextView) convertView
                .findViewById(R.id.retailer_list_item_tv_slogan);
        holder.msgDesc = (TextView) convertView
                .findViewById(R.id.retailer_list_item_tv_msg);
        holder.typeImage = (ImageView) convertView
                .findViewById(R.id.retailer_list_item_iv_type);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    //downloading image
    imageLoader.DisplayImage(Perma.uImageRetailer[position],
            holder.retailerImage);

    holder.retailerName.setText(rowItem.getuNameRetailer());
    holder.retailerSlogan.setText(rowItem.getuSloganRetailer());
    holder.msgDesc.setText(rowItem.getuDescRetailer());
    holder.typeImage.setImageResource(rowItem.getUtypeMsg());

    return convertView;
}

}

这是我的主要活动。

ListView retailListView;
List<RowItem> retailRowItems;
CustomListViewAdapterRetailList adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.retailer_list);

    lSearch = (RelativeLayout) findViewById(R.id.rlist_ll_top);
    searchBox = (EditText)findViewById(R.id.rlist_et_search);
    startSearch = (Button)findViewById(R.id.rlist_bt_search_initiate);

    retailRowItems = new ArrayList<RowItem>();

    Log.i("in retailer list",""+Perma.retailerArrayLength);
    for (int i = 0; i < Perma.retailerArrayLength; i++) {
        Log.i("in retailer list","infor");
        RowItem item = new RowItem(Perma.uNameRetailer[i],
                Perma.uSloganRetailer[i], Perma.uImageRetailer[i],
                Perma.uDescRetailer[i], Perma.typeRetailer[i]);
        Log.i("retailer list",""+Perma.uNameRetailer[i]+""+Perma.uImageRetailer[i]);
        retailRowItems.add(item);
    }

    retailListView = (ListView) findViewById(R.id.rlist_lv);


    // i tried to clear data but it clears whole list and does not populate with new data.
    /*   if(adapter!=null){
        adapter.clear();}
            */

    adapter = new CustomListViewAdapterRetailList(this,
            R.layout.retailer_list_list_item, retailRowItems);

    retailListView.setAdapter(adapter);     

}


@Override
protected void onResume() {
    super.onResume();


    retailListView.invalidateViews();
    adapter.notifyDataSetChanged();

}

现在的事情是 用户单击按钮,出现一个对话框,用户选择复选框并单击确定。 然后加载新数据。

我试过clear(),invalidate()和notifyDataSetChanged() 但他们没有工作。

请在这里帮助我,请在我的代码背景下给出答案,因为我还是新手。

提前致谢!!

2 个答案:

答案 0 :(得分:0)

这是答案。

protected void onResume() {
    super.onResume();


 /// here i used it in resume because my data was changing because of user interaction with dialog. so its should be in in resume.

    if(!retailRowItems.isEmpty()){
    ///now if row is populated clear its data
        retailRowItems.clear();

            ///populate again  
    for (int i = 0; i < Perma.retailerArrayLength; i++) {

        RowItem item = new RowItem(Perma.uNameRetailer[i],
                Perma.uSloganRetailer[i], Perma.uImageRetailer[i],
                Perma.uDescRetailer[i], Perma.typeRetailer[i]);

        retailRowItems.add(item);       

    }

    }
            ///notify adapter
    adapter.notifyDataSetChanged();
}

答案 1 :(得分:0)

您还可以覆盖适配器的notifyDataSetChanged()方法:

@Override
public void notifyDataSetChanged() {
   super.notifyDataSetChanged();
   //refresh your List<RowItem> objects here
}