I deduce from DB a word in ListView by a custom adapter. The word can be chosen. In the custom listview there is a button by clicking on which you can make the word favorites or remove them from favorites. It seems that everything works, but there is a problem: I clicked on the add button to add a word, the icon changed, then I scrolled up or down the list and projected to the list item that I had chosen, but the icon remained added to favorites, although in fact added and changes occurred in the database. Here is the code for my adapter. Tell me where did I miss that?

public class WordsAdapter extends BaseAdapter { Context ctx; LayoutInflater lInflater; ArrayList<SingleWord> words; WordsAdapter(Context context, ArrayList<SingleWord> words) { ctx = context; this.words = words; } @Override public int getCount() { return words.size(); } @Override public Object getItem(int position) { return words.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; SingleWord p = getSingleWord(position); if (convertView == null){ LayoutInflater inflater = (LayoutInflater) ctx .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.item, parent, false); viewHolder = new ViewHolder(); viewHolder.txtAlphabet = (TextView) convertView.findViewById(R.id.txtAlphabet); viewHolder.txtWord = (TextView) convertView.findViewById(R.id.txtWord); viewHolder.btnFavorites = (ImageButton) convertView.findViewById(R.id.btnFavorites); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.txtWord.setText(p.word); viewHolder.txtAlphabet.setText(p.alphabet); viewHolder.btnFavorites.setImageResource(p.bntIcon); viewHolder.btnFavorites.setFocusable(false); viewHolder.favorites = p.favorites; if (viewHolder.favorites.equals("1")) { viewHolder.btnFavorites.setImageResource(R.drawable.icon_star_yellow); } else if (viewHolder.favorites.equals("0")) { viewHolder.btnFavorites.setImageResource(R.drawable.icon_star_outline_black); } viewHolder.btnFavorites.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String selectedItem = spinner.getSelectedItem().toString(); if (selectedItem.equals("что-то")) { if (viewHolder.favorites.equals("1")) { String wordStr = viewHolder.txtWord.getText().toString(); ContentValues values = new ContentValues(); values.put(SlovarEntry.COLUMN_FAVORITES, "0"); long newRowId = db.database.update(SlovarEntry.TABLE_RUS, values, SlovarEntry.COLUMN_WORD + "= ?", new String[]{wordStr}); viewHolder.favorites = "0"; if (newRowId == -1) { // Если ID -1, значит произошла ошибка Toast.makeText(ctx, "Ошибка", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(ctx, "Удалено из избранное", Toast.LENGTH_SHORT).show(); viewHolder.btnFavorites.setImageResource(R.drawable.icon_star_outline_black); } } else if (viewHolder.favorites.equals("0")){ String wordStr = viewHolder.txtWord.getText().toString(); ContentValues values = new ContentValues(); values.put(SlovarEntry.COLUMN_FAVORITES, "1"); long newRowId = db.database.update(SlovarEntry.TABLE_RUS, values, SlovarEntry.COLUMN_WORD + "= ?", new String[]{wordStr}); viewHolder.favorites = "1"; if (newRowId == -1) { // Если ID -1, значит произошла ошибка Toast.makeText(ctx, "Ошибка", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(ctx, "Добавлено в избранное", Toast.LENGTH_SHORT).show(); viewHolder.btnFavorites.setImageResource(R.drawable.icon_star_yellow); } } } } }); return convertView; } SingleWord getSingleWord(int position) { return ((SingleWord) getItem(position)); } } static class ViewHolder { TextView txtAlphabet; TextView txtWord; ImageButton btnFavorites; String favorites; } 

Class for list item

 public class SingleWord { String word; String alphabet; int bntIcon; String favorites; SingleWord(String w, String a, String f, int b) { word = w; alphabet = a; bntIcon = b; favorites = f; } public String getWord() { return word; } public String getAlphabet() { return alphabet; } public String getFavorites() { return favorites; } } 

    1 answer 1

    The problem is that the words array doesn't know when you added or removed a word from your favorites. You need the value of the field favorites for the word and then everything will be ok. ListView keeps several elements in memory and when scrolling it re-creates a selection, but as a value in the array did not hear anything about the change, this is how it turns out

    • Do you mean using the setter to set the value of the favorites variable after the value has been updated in the database? - Collective Farmer
    • Made through the setter update the favorites field, but did not help. I do so p.setFinding ("0"); where p is an object of type SingleWord - Kolkhoznik
    • SingleWord p = getSingleWord (position); this is a local reload and all that you do with it will give nothing. you need something like: words.get (position) .setFlicit (1 or 0) and place it inside the click box - Andriy Martsinkevych