How to put only real items in the second Activity?

0

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.

    
asked by anonymous 13.01.2018 / 18:51

1 answer

1

All Well Welyson.

I recommend that you use RecyclerView's instead of ListView's.
Follow here a good tutorial.
< br> As for passing the actual items to 2nd activity, both using RecyclerView and ListView, you will need to access the Adapter and check the "true" items and pass them to 2nd activity via "putExtras":

I noticed that your code is not very objective. I'll note some corrections in order for you to proceed:

This part of the code does not seem to be 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 the button:

View.OnClickListener abreFavoritos = new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        for(int i=0;i<lv.getAdapter().getCount();i++){
            memes fMeme = (memes)lv.getAdapter().getItem(i));
            if(item.isFavorito()) favoriteMemes.add(fMeme);
        }
        Intent intent = new Intent(this, Favoritos.class);
        //Passe a List<memes> como parâmetro extra para a 2a Activity
        intent.putExtra("favoritos", new Gson().toJson(favoriteMemes));

        //Esta parte esta faltando
        startActivity(intent);
    }
}

To retrieve the list in 2nd activity use:

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //Recupere a Array com os Favoritos
        String memesString =getIntent().getStringExtra("favoritos");
        memes[] fMemes=new Gson().fromJson(memesString,memes[].class);
    }

I hope I have helped.

    
31.01.2018 / 14:48