0
I’m trying to make a favorite activity, I’ll follow the example of this guy here. In the second Activity I will create how I would put these items set as true in a listview? and play them normally I would have to recreate the entire playback code in the second Activity? and then how would I remove them. This is a favorite scheme if anyone knows a good tutorial is already a great help, thanks.
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 that I am well behind the goal, in case n have time indicate tutorials or links of what I need to study to do this.
Thanks I’ll try and come back here, vlw msm was looking for this for a while
– Welyson
Your explanation sounds great but I’m a little lost, I updated the post with my code, when you have a little time can help me or indicate tutorials so I can fill the lack of knowledge? Thank you
– Welyson
I noticed your code isn’t very objective. I will mark some corrections in order for you to proceed: This code delivery does not seem in line with your goal. I believe you have programmed a button or menu to access your 2nd activity, and if so, create an Onclicklistener and associate with the button: View.Onclicklistener opens Views = new View.Onclicklistener(){ }
– Maicon Coelho
ei Maicon the items are not being displayed in the second Activity updated the code of a look E tbm in case I manage to arrange as I would in Activity Favorites remove them
– Welyson
Remove the item from your Adapter and notify the removal.
– Maicon Coelho
Blz, but the items that are not being displayed, could look at my code?
– Welyson
Which IDE are you using, Eclipse or Android Studio? I’ll send you an example to analyze.
– Maicon Coelho
android studio Thanks
– Welyson
Give me an email address so I can send you a project template for you to base.
– Maicon Coelho
[email protected]
– Welyson