Listview not updating based on value on Array Adapter

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Listview not updating based on value on Array Adapter



I call the method below to update my listview


ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
ListAdapter adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);



but that code is associated with a value that changes and also this is the other code


private ArrayList<plan_model> getItems_search(String param_cusname)
Cursor data = myDb.get_search_plan(pattern_email, param_name);
int i = 0;
while (data.moveToNext())
String date = data.getString(3);
String remarks = data.getString(4);
items.add(new plan_model(cusname, remarks);

return items;



and this is my sorter


private ArrayList sortAndAddSections(ArrayList<plan_model> itemList)

Collections.sort(itemList);
plan_model sectionCell;

tempList.clear();
tmpHeaderPositions.clear();

String header = "";
int addedRow = 0;
int bgColor = R.color.alt_gray;
for (int i = 0; i < itemList.size(); i++)

String remarks = itemList.get(i).getRemarks();
String date = itemList.get(i).getDate();

if (!(header.equals(itemList.get(i).getDate())))
sectionCell = new plan_model(remarks, date);
sectionCell.setToSectionHeader();
tmpHeaderPositions.add(i + addedRow);
addedRow++;
tempList.add(sectionCell);
header = itemList.get(i).getDate();
bgColor = R.color.alt_gray;

sectionCell = itemList.get(i);
sectionCell.setBgColor(bgColor);
tempList.add(sectionCell);
if (bgColor == R.color.alt_gray) bgColor = R.color.alt_white;
else bgColor = R.color.alt_gray;


tmpHeaderPositions.add(tempList.size());
for (int i = 0; i < tmpHeaderPositions.size() - 1; i++)
sectionCell = tempList.get(tmpHeaderPositions.get(i));
sectionCell.setDate(sectionCell.getDate() + " (" +
(tmpHeaderPositions.get(i + 1) - tmpHeaderPositions.get(i) - 1) + ")");


return tempList;



my question is the value name changes but my listview is not how can I update my listview? because i need to update it based on search parameter


name





Can you please post the code snippet which you used to call the notifydatasetchanged, including the data updating part.
– Sudhi
Aug 8 at 13:59





actually i dont have notifydatasetchanged and the data update comes from the edittext that will be pass in my DatabaseHelper
– myown email
Aug 8 at 14:01


notifydatasetchanged


edittext


DatabaseHelper




2 Answers
2



In getItems_search() add this line at the beginning:


getItems_search()


items.clear();



Every time the value of name changes you have to do the following:


itemsList.clear();
itemsList = sortAndAddSections(getItems_search(name));
list.setAdapter(new ListAdapter(getActivity(), itemsList));





this one also add the rows only
– myown email
Aug 8 at 14:19





it can't add because of itemsList.clear() at the beginning. So if you see items added then there's something wrong in sortAndAddSections(getItems_search(name));
– user8959091
Aug 8 at 14:21


itemsList.clear()


sortAndAddSections(getItems_search(name));





currently checking
– myown email
Aug 8 at 14:25





nothing happens the listview doesnt refresh
– myown email
Aug 8 at 14:28





So for sure there is a problem in sortAndAddSections(getItems_search(name));
– user8959091
Aug 8 at 14:30


sortAndAddSections(getItems_search(name));



If your itemList is being updated properly, you don't need to create another instance of the adapter, just use notifyDataSetChanged():


itemList


notifyDataSetChanged()


private void createList()
ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);


private void updateList()
sortAndAddSections(getItems_search(name)); // Update itemList without re-assign its value, otherwise the adapter will loose reference
adapter.notifyDataSetChanged()





it only adds the data instead of replacing it
– myown email
Aug 8 at 14:07





If I understand correctly, in this case you can clear your array before adding the other values.
– Alan Raso
Aug 8 at 14:09





huuummm let me check
– myown email
Aug 8 at 14:11





what array do i need to clear?>
– myown email
Aug 8 at 14:15





itemList. But don't do itemList = something, just update its items
– Alan Raso
Aug 8 at 14:17


itemList = something






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS