How to put only true items in the second Activity?

Asked

Viewed 76 times

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.

1 answer

1

All right Welyson.

I recommend using Recyclerview’s instead of Listview’s.
Follows here a good tutorial.

As to pass the true items to the 2nd Activity, both using Recyclerview and in 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 will mark some corrections so that you give continuation:

This part of the code doesn’t seem in line with its purpose. I believe you have programmed a button or menu to access your 2nd activity, and if so, create an Onclicklistener and associate to 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 use Activity:

    @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’ve helped.

  • Thanks I’ll try and come back here, vlw msm was looking for this for a while

  • 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

  • 1

    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(){ }

  • 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

  • 1

    Remove the item from your Adapter and notify the removal.

  • Blz, but the items that are not being displayed, could look at my code?

  • 1

    Which IDE are you using, Eclipse or Android Studio? I’ll send you an example to analyze.

  • android studio Thanks

  • Give me an email address so I can send you a project template for you to base.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.