0

I don't know why but my list view does not get updated...I am calling notifyDataSetChanged() and I also tried another answer where they asked to do this :

 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);

    }
}

this is in my custom adapter.

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;
}

}

I dont know why is my listview not being updated immediately...Should i switch to ArrayAdapters?

Sharang Gupta
  • 367
  • 1
  • 2
  • 15

4 Answers4

1

Try to clear your this.historyItems then this.historyItems.addAll(historyItems). Hope it works!

Anyway if you try ViewHolder Pattern, you should reuse it:

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

// fill data

maybe its the reason why your listview didnt reflect immediately.

linhtruong
  • 441
  • 3
  • 12
1

When you assign a new ArrayList to this.historyItems, the data can no longer be notified because you've lost the reference to the Arraylist that you specified originally.

If you want that method to work, you need to override getCount and getItem.

But you also need to

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

Or, if using an ArrayAdapter, you don't need the list of as a member variable

clear();
addAll(historyItems);

Then, notifyDatasetChanged should work

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I am doing this : this.historyItems.clear(); this.historyItems.addAll(historyItems); Also , getCount and getItem are overridden... – Sharang Gupta Nov 03 '16 at 14:30
  • @Override public int getCount() { return historyItems != null ? historyItems.size() : 0; } public Object getItem(int position) { return historyItems.get(position); } – Sharang Gupta Nov 03 '16 at 14:32
  • Looks good to me. To stop us from guessing at the problems, then, you have to show a [mcve] in your post – OneCricketeer Nov 03 '16 at 14:37
  • Is my code snippet fine now @cricket_007 ? please take a look....Thank you – Sharang Gupta Nov 03 '16 at 14:47
  • Can you try not having the adapter as an inner class? I think you might be crossing the references to the Arraylist – OneCricketeer Nov 03 '16 at 14:52
  • It just appears so...its in a different class...it is not an inner class...I actually have more functions in my main which is why the class end race is not there... – Sharang Gupta Nov 03 '16 at 14:57
  • Personally, I see nothing too wrong with what you have. You can try adding sample data to the adapter in onCreate, and call refresh. Should work fine. The problem may be onActivityResult is clearing a reference to the list somehow – OneCricketeer Nov 03 '16 at 15:02
  • I tried using ArrayAdapter instead of baseadapter , and i used addAll(historyItems) and now i have a different issue...the item is being updated but the older array also shows in my listview, that is all the items are duplicated...Any ideas? – Sharang Gupta Nov 03 '16 at 15:05
  • The problem is when i use clear first i get a blank screen...and without clear i get duplicates!!! This is soo frustrating!!! – Sharang Gupta Nov 03 '16 at 15:08
  • If you add data after clearing, and finally end with a notifyDatasetChanged, then I don't understand how that situation could happen. – OneCricketeer Nov 03 '16 at 15:10
0

Try to .setAdapter(adapter) before call adapter.notifyDataSetChanged();

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rodrigo Paixão
  • 246
  • 5
  • 12
0

Please check this method

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

Do this. It will work

Chandan kushwaha
  • 941
  • 6
  • 26