即使在调用notifydatasetchanged()之后我的列表视图也没有更新

时间:2016-11-03 14:15:21

标签: java android listview android-adapter

我不知道为什么但是我的列表视图没有得到更新...我正在调用notifyDataSetChanged(),我还尝试了另一个答案,他们要求这样做:

 private void editTransaction(int position) {
    h = historyItems.get(position);
    historyItems.remove(position);
    this.position = position;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 5 && resultCode == RESULT_OK) {
        String description = data.getStringExtra("description");
        if (description == null) description = "no description";

        double amount = Double.parseDouble(data.getStringExtra("amount"));

        h.setDescription(description);
        h.setAmount(amount);
        //h.setTimestamp(System.currentTimeMillis());

        //this is where I am updating my arraylist....

        historyItems.add(position,h);
        adapter.notifyDataSetChanged();


        user.makeEdit(h);

    }
}

这是我的自定义适配器。

public class HistoryCustomAdapter extends BaseAdapter {

Context context;
ArrayList<HistoryItem> historyItems;
HashMap<String,String> iconMapper;

private static LayoutInflater inflater=null;

public HistoryCustomAdapter(Context context, ArrayList<HistoryItem> historyItems, HashMap<String, String> iconMapper) {
    this.context=context;
    this.historyItems=historyItems;
    this.iconMapper=iconMapper;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return historyItems != null ? historyItems.size() : 0;
}

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

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

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

    Holder holder;
    if (convertView == null) {
        holder = new Holder();
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.history_item, parent, false);

        holder.tvCat = (TextView)convertView.findViewById(R.id.tv_category);
        holder.tvAmt = (TextView)convertView.findViewById(R.id.tv_amount);
        holder.tvDate = (TextView)convertView.findViewById(R.id.tv_date);
        holder.img = (ImageView)convertView.findViewById(R.id.iv);
        convertView.setTag(holder);

    }

    holder=(Holder)convertView.getTag();
    HistoryItem historyItem;
    historyItem = historyItems.get(position);

    holder.tvCat.setText(historyItem.getCategory());
    holder.tvAmt.setText(historyItem.getAmount()+"");

    DateFormat df = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss");
    Calendar c    = Calendar.getInstance();
    c.setTimeInMillis(historyItem.getTimestamp());
    Date day      = c.getTime();

    holder.tvDate.setText(df.format(day));

    Resources res = context.getResources();
    String mDrawableName = iconMapper.get(historyItem.getCategory());
    int resID = res.getIdentifier(mDrawableName , "drawable", context.getPackageName());
    holder.img.setImageResource(resID);

    return convertView;
}

public void refresh(ArrayList<HistoryItem> historyItems)
{
    this.historyItems.clear();
    this.historyItems.addAll(historyItems);
    //notifyDataSetChanged();
}

public class Holder
{
    TextView tvCat,tvAmt,tvDate;
    ImageView img;
}

}

我不知道为什么我的listview没有立即更新...我应该切换到ArrayAdapters吗?

4 个答案:

答案 0 :(得分:1)

尝试清除this.historyItems然后this.historyItems.addAll(historyItems)。希望它有效!

无论如何,如果您尝试使用ViewHolder Pattern,您应该重复使用它:

if (rowView == null) {
   // init view, holder
   rowView.setTag(holder)
} else {
   holder = (Holder) rowView.getTag();
}

// fill data

也许是你的列表视图没有立即反映的原因。

答案 1 :(得分:1)

当您将新的ArrayList分配给this.historyItems时,由于您丢失了对最初指定的Arraylist的引用,因此无法再通知数据。

如果您希望该方法有效,则需要覆盖getCountgetItem

但你还需要

this.historyItems.clear();
this.historyItems.addAll(historyItems);

或者,如果使用ArrayAdapter,您不需要作为成员变量的列表

clear();
addAll(historyItems);

然后,notifyDatasetChanged应该工作

答案 2 :(得分:0)

在致电.setAdapter(adapter)

之前尝试adapter.notifyDataSetChanged();

答案 3 :(得分:0)

请检查此方法

public void refresh(ArrayList<HistoryItem> historyitems)
{
    this.historyItems=historyitems;
    notifyDataSetChanged();
}

这样做。它会起作用