The code below saves the current text of the TextView when the favourite button is clicked and the button Image changes to Fav_Checked when I click again the favourite button is unchecked and the text in array is removed but the issue is when I scroll down the ListView updates and the favourite buttons are unchecked. kindly help.
public class MainActivity extends AppCompatActivity {
// ArrayList<String> names = new ArrayList<>();
private ArrayList<String> names;
int x = names.size();
// private boolean[x] favorites;
private SharedPreferences sharedPreferences;
Context context = this;
MediaPlayer mPlayer;
Boolean isPlay = true;
ImageView playPauseGlobelBtn;
int maxVolume,resID,position;
String fname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView)findViewById(R.id.listView);
mPlayer = MediaPlayer.create(context, R.raw.asdf);
CustomAdapter customAdapter=new CustomAdapter();
listView.setAdapter(customAdapter);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String favoriteItems = sharedPreferences.getString("FAVORITE_ITEMS", "");
if(favoriteItems.isEmpty()) {
names = new ArrayList<>();
}
else {
names = new ArrayList<>(Arrays.asList(favoriteItems.split(",")));
}
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.customlayout, null);
final TextView textView_name = (TextView)
view.findViewById(R.id.textView_name);
final Button favoritebutton = (Button) view.findViewById(R.id.tgbFav);
buttonInfo.setOnClickListener(new View.OnClickListener() {
String tag = v.getTag().toString();
if(tag != null) {
int i = Integer.parseInt(tag);
System.out.println(tag);
}
}
});
private ArrayList<String> names;
private SharedPreferences sharedPreferences;
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String favoriteItems = sharedPreferences.getString("FAVORITE_ITEMS", "");
if(favoriteItems.isEmpty())
names = new ArrayList<>();
else
names = new ArrayList<>(Arrays.asList(favoriteItems.split(",")); //Update like this
favoritebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View v) {
if (!names.contains(textView_name.getText())){
names.add((String) textView_name.getText());
for (int i=0; i<names.size(); i++) {
System.out.println(names.get(i));
favoritebutton.setBackgroundResource(R.drawable.fav_checked);
}
}
else {
System.out.println(textView_name.getText() + " is already present in the Array at index " + names.indexOf(textView_name.getText()));
int currentIndex = names.indexOf(textView_name.getText());
names.remove(currentIndex);
for (int i=0; i<names.size(); i++) {
System.out.println(names.get(i));
favoritebutton.setBackgroundResource(R.drawable.star_off);
}
}
sharedPreferences.edit().putString("FAVORITE_ITEMS", TextUtils.join(",", names)).apply();
}
});
<ToggleButton
android:id="@+id/tgbFav"
android:layout_width="30dp"
android:layout_height="30dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:layout_toLeftOf="@+id/button"
android:textOn=""/>