InQBarna / TableFixHeaders

Android library that implements a table with fixed headers.
Apache License 2.0
818 stars 300 forks source link

adding a new item or updating existing item with notifydatasetchanged #98

Open gdubs opened 8 years ago

gdubs commented 8 years ago

Hi there, I've been struggling on how to properly update the current data with the fixed header. Whenever I would add an item, I would try to do the following:

So I can add a new string I started with a List<String[]> which I convert. I'm not sure if that's the right way but it seems to update the array String[][] with the updated data. My issue is it does not update the loaded table using notifyDataSetChanged as mentioned in this >> https://github.com/InQBarna/TableFixHeaders/issues/29

public void addItem(Item item){
        if(item != null){
            items.add(item);

            String[] itemStrings = new String[5];
            itemStrings[0] = item.Id;
            itemStrings[1] = item.Description;
            itemStrings[2] = item.Quantity.toString();
            itemStrings[3] = item.UnitValue.toString();
            itemStrings[4] = item.Comment.Name;

            matrixGeneratedList.add(itemStrings);

            matrixGeneratedConverted = matrixGeneratedList.toArray(new String[matrixGeneratedList.size()][]);
        }
        matrixTableAdapter.notifyDataSetChanged();
    }

Also, what about on update?

Thank you!

BraisGabin commented 8 years ago

I can't know what's wrong in your code, this code can work. Any way, some tips: Why do you use two different data structures with the same data? You can use the List<String[]> inside the adapter. Don't change the adapter's data structure outside the adapter. Don't call dataSetChanged from outside. The adapter must handle his own data and he must know when it must notify the changes. I think that if you do this changes you will find the problem.

gdubs commented 8 years ago

I think I found it. I had to refresh the data inside the adapter. I'm using the simple version so I call the setInformation. But I tend to call it outside of the adapter. So I'm guessing that's wrong? Or if it's right, should I call the datasetchanged inside that method after updating?

nkVivek commented 6 years ago

I have same problem with it. notifyDataSetChanged() method not working when Updating existing data in adapter.