簡體   English   中英

即使調用notifydatasetchanged()后,我的列表視圖也沒有更新

[英]My listview is not being updated even after calling notifydatasetchanged()

我不知道為什么,但是我的列表視圖沒有更新...我在調用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;
}

}

我不知道為什么我的列表視圖沒有立即更新...我應該切換到ArrayAdapters嗎?

嘗試先清除this.historyItems然后清除this.historyItems.addAll(historyItems) 希望它能起作用!

無論如何,如果您嘗試使用ViewHolder Pattern,則應該重用它:

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

// fill data

也許這就是您的列表視圖沒有立即反映出來的原因。

當您向this.historyItems分配新的ArrayList時,將不再通知數據,因為您丟失了對最初指定的Arraylist的引用。

如果希望該方法起作用,則需要重寫getCountgetItem

但是你還需要

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

或者,如果使用ArrayAdapter,則不需要將列表作為成員變量

clear();
addAll(historyItems);

然后,notifyDatasetChanged應該工作

在調用adapter.notifyDataSetChanged();之前嘗試.setAdapter(adapter) adapter.notifyDataSetChanged();

請檢查此方法

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

做這個。 會工作的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM