I'm trying to make a Favorites Activity, I'll follow the example of this guy here . In the second Activity I will create as I would to put these items set as true in a listview? and playing them normally would I have to re-create all the playback code in the second Activity? and then how would I remove them. This is a favorites scheme if anyone knows a good tutorial is already a big help, thank you.
MainActivity.class
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
ArrayList<memes> item;
ArrayAdapter<memes> arrayAdapter;
String[] Nomes = new String[]{"Compartilhar meme", "Favoritos"};
List<memes> favoriteMemes= new ArrayList<memes>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
isStoragePermissionGranted();
lv = findViewById(R.id.lv);
mp = new MediaPlayer();
item = new ArrayList<>();
//itens
item.add(new memes("Arnold Musica", R.raw.diffrentstrokes));
item.add(new memes("Aham sei ", R.raw.ahamsei));
item.add(new memes("2 mil anos", R.raw.milanos));
item.add(new memes("Acelera jesus", R.raw.acelera_jesus));
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);
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Olá, Marilene!");
builder.setItems(MainActivity.this.Nomes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
sendWhatsAppAudio(position);
return;
case 1:
item.get(position).setmIsFavourite(true);
return;
default:
}
}
});
builder.show();
return true;
}
});
//Aqui esta a bagunça
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.abreFavoritos:
for(int i=0;i<lv.getAdapter().getCount();i++){
memes fMeme = (memes)lv.getAdapter().getItem(i);
//mIsFavourite em meu codigo fica vermelho
//o que eu devo colocar depois do item. para que funcione?
if (item.mIsFavourite()) {
favoriteMemes.add(fMeme);
}
}
intent = new Intent(this, Favoritos.class);
intent.putExtra("favoritos", new Gson().toJson(favoriteMemes));
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Favorites.class
public class Favoritos extends AppCompatActivity {
ListView lv;
ArrayAdapter<memes> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
lv = findViewById(R.id.lv);
String memesString = getIntent().getStringExtra("favoritos");
memes[] fMemes = new Gson().fromJson(memesString,memes[].class);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fMemes);
lv.setAdapter(arrayAdapter);
}
}
memes.class (Adapter)
public class memes{
private String nome;
private int resID;
private Boolean mIsFavourite;
public memes(String nome, int resID){
this.nome = nome;
this.resID = resID;
}
public Boolean getmIsFavourite() {
return mIsFavourite;
}
public void setmIsFavourite(Boolean mIsFavouriteResource) {
this.mIsFavourite = mIsFavouriteResource;
}
public String getNome(){
return nome;
}
public int getResId(){
return resID;
}
@Override
public String toString(){
return nome;
}
}
It seems like I'm well behind the goal, if I do not have time to tell tutorials or links that I need to study to do this.