0

这是我的适配器类。

 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(),但它们不起作用。

请在这里帮助我,并请在我的代码上下文中给出答案,因为我还是一个新手。

提前致谢 !!

4

2 回答 2

0

Here is the answer.

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();
}
于 2013-05-02T06:56:17.663 回答
0

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

@Override
public void notifyDataSetChanged() {
   super.notifyDataSetChanged();
   //refresh your List<RowItem> objects here
}
于 2013-11-08T14:20:55.777 回答