I can not do the following. In general, there is the main MainActivity, in which the list of words from the database (each item in the list consists of TextView and ImageButton ) and WordActivity is output through the custom ListView . WordActivity has an Add / Remove from Favorites button that adds or removes a word from the favorites list. The problem is as follows. If in WordActivity, to delete a word from the list of favorites, and return to MainActivity, then the icon of the ImageButton in the custom ListView the corresponding word does not change. How do I get the icon to change?
public class WordsAdapter extends BaseAdapter { Context ctx; ArrayList<SingleWord> words; //ΠΠ±ΡΠ΅ΠΊΡ ΠΊΠ»Π°ΡΡΠ° SingleWord SingleWord p; //ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠ»Π°ΡΡΠ° WordsAdapter 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(final int position, View convertView, ViewGroup parent) { //ΠΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ ΡΠΈΠΏΠ° ViewHolder 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 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(); } //ΠΡΡΠ°Π²Π»ΡΠ΅ΠΌ Π² ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠΈΠ΅ TextView ΠΈ ImageButton Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΠΊΠ»Π°ΡΡΠ° SingleWord viewHolder.txtWord.setText(p.word); viewHolder.txtAlphabet.setText(p.alphabet); viewHolder.btnFavorites.setImageResource(p.bntIcon); viewHolder.btnFavorites.setFocusable(false); //ΠΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ Π΅ΡΠ»ΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ favorites ΠΈΠ· ΠΊΠ»Π°ΡΡΠ° SingleWord = 1, ΡΠΎ ΠΎΠ΄Π½Π° ΠΈΠΊΠΎΠ½ΠΊΠΈ, ΠΈΠ½Π°ΡΠ΅ Π΄ΡΡΠ³Π°Ρ if (words.get(position).getFavorites().equals("1")) { viewHolder.btnFavorites.setImageResource(R.drawable.icon_star_yellow); } else if (words.get(position).getFavorites().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("ΡΡΠΎ-ΡΠΎ")) { //ΠΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ ΠΎΠΏΡΡΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ favorites if (words.get(position).getFavorites().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}); words.get(position).setFavorites("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 (words.get(position).getFavorites().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}); words.get(position).setFavorites("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; }