Apologies in advance if this is a dumb question. I have searched the web for an answer to my issue, but have not found anything that would help.
I have a ListView and a CustomViewAdapter within a Fragment. This works fine. The list is displayed correctly.
What I have a problem with is if a row within the list changes, although the row gets updated, the actual list does not. To put it in context, what I have is a list of favorites and they are shown in a favorites fragment. However, if the user "unfavorites" an item, even though the icon that shows it is a favorite change, the actual list still shows that item, rather than refresh the list and remove that item from the list.
As I said, I have tried all the solutions I could find related to this issue, such as calling notifyDataSetChanged with the adapter.
My code in the fragment:
public void ShowFavourites(){
globalVariable = ((GlobalClass) getActivity().getApplicationContext());
JCDatabaseHelper helper = new JCDatabaseHelper(getActivity().getBaseContext());
ArrayList<ArticleItem> rowItems = new ArrayList<>();
ArticleItem myItem1;
String[] columnNames = {"_id", "Column1", "Column2", "Column3",
"Column4", "Column5", "Column6", "Column7"};
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_id",
"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7" });
int[] to = new int[]{R.id.id, R.id.imageButton, R.id.title, R.id.year, R.id.authors,
R.id.favs, R.id.read, R.id.readOnLine};
final ListView listView = (ListView) getActivity().findViewById(android.R.id.list);
ArrayList<Favourites> favouritesArrayList = helper.GetFavourites();
for (Favourites articleItem : favouritesArrayList) {
// code to access items
//then...
matrixCursor.addRow(new Object[]{k, imgFile,Title, Year , Authors, favIcon , btn1.getText(), btn2.getText() });
myItem1 = new ArticleItem(ID, Title, Year);
rowItems.add(myItem1);
}
CustomListViewAdapter customAdapter;
ListView yourListView;
customAdapter = new CustomListViewAdapter(getActivity().getBaseContext(),
R.layout.article_row, rowItems);
yourListView.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
}
The code in CustomListViewAdapter:
public class CustomListViewAdapter extends ArrayAdapter<ArticleItem> {
ArrayList<ArticleItem> _rowItems;
ArticleItem item;
View row;
public CustomListViewAdapter(Context context, int resourceId,
ArrayList<ArticleItem> articleItems) {
super(context, resourceId,articleItems);
this.context = context;
_rowItems = articleItems;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
row = convertView;
item = _rowItems.get(position);
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.article_row, parent, false);
}
// other code to set TextViews, etc ...
final int FavYes = R.drawable.favourite;
final int FavNo = R.drawable.favourite_outline;
final ImageButton favBtn = (ImageButton) row.findViewById(R.id.favs);
if (Favs.equals("Y")){
favBtn.setImageResource(FavYes);
}
else favBtn.setImageResource(FavNo);
favBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JCDatabaseHelper helper = new JCDatabaseHelper(getContext());
item = _rowItems.get(position);
ID = item.getID();
Title = item.getTitle();
String NewFavs;
if (Favs.equals("N")){
NewFavs = "Y";
favBtn.setImageResource(FavYes);
helper.UpdateFavourite("FavouritesTable", ID, NewFavs);
}
else {
NewFavs = "N";
favBtn.setImageResource(FavNo);
helper.UpdateFavourite("FavouritesTable", ID, NewFavs);
// THIS ACTION SHOULD TAKE THIS ROW FROM THE LIST AND RESHOW THE LIST.
}
}
});
return row;
}
I have tried the following answers: 1. How to refresh Android listview? but it doesn't work. Maybe because I don't call it correctly. 2. notifyDataSetChanged() in Fragment is not refreshing listview Other questions/answers were dealing with similar, but not the same issue.
Any help will be much appreciated. Thanks