I have here an arraylist that is shown in a listview, when I give a long click a dialog is displayed and contains the favorite option that when it is selected the object is marked as true, how do I display only the items marked in my ActivityFavorites ?
MainActivity.class
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
ArrayList<memes> item;
ArrayAdapter<memes> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
lv = findViewById(R.id.lv);
mp = new MediaPlayer();
item = new ArrayList<>();
//itens
item.add(new memes("Fique apertado sobre o meme para compartilhar", R.raw.sharebagui));
item.add(new memes("2 mil anos", R.raw.milanos));
item.add(new memes("Acelera jesus", R.raw.acelera_jesus));
item.add(new memes("Azideia", R.raw.asideia));
item.add(new memes("Acertou mizeravi", R.raw.mizeravi));
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(arrayAdapter);
//play audio
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
playSong(position);
}
});
//PROGRESS
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
//PROGRESS
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Olá, Marilene!");
builder.setItems(Nomes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // Delete
sendWhatsAppAudio(position);
break;
case 1: // Copy
item.get(position).setmIsFavourite(true);
break;
default:
break;
}
}
});
AlertDialog alertDialog = builder.create();
builder.show();
return true;
}
});
}
public void playSong(int songIndex) {
mp.reset();
mp = MediaPlayer.create(this, arrayAdapter.getItem(songIndex).getResId());
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
memes.class
public class memes{
private String nome;
private int resID;
private Boolean mIsFavourite;
memes(String nome, int resID){
this.nome = nome;
this.resID = resID;
}
public String getNome(){
return nome;
}
int getResId(){
return resID;
}
@Override
public String toString(){
return nome;
}
public Boolean getmIsFavourite() {
return mIsFavourite;
}
public void setmIsFavourite(Boolean mIsFavouriteResource) {
this.mIsFavourite = mIsFavouriteResource;
}
}
ActivityFavorites.class
public class ActivityFavoritos {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_favoritos);
lv = findViewById(R.id.lvf);
if (list.get(position).getmIsFavourite()) {
//do want you want when its true
} else {
//do want your code when its false.
}
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(arrayAdapter);
}
}