如何仅在新列表视图中放置真实对象

时间:2017-12-28 02:36:57

标签: java android arraylist

我这里有一个列表视图中显示的arraylist,当我长按一个对话框显示它包含喜欢的选项,当选中对象被标记为true时,我如何只显示我标记的项目ActivityFavoritos?

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;
    }

}

到目前为止,这是我的进步,我对我最喜欢的活动应该做些什么感到困惑。

ActivityFavoritos.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);
    }
}

1 个答案:

答案 0 :(得分:1)

memes class 声明变量

private Boolean mIsFavourite;

并声明getter和setter方法。

  public Boolean getmIsFavourite() {
        return mIsFavourite;
    }

    public void setmIsFavourite(Boolean mIsFavouriteResource) {
        this.mIsFavourite = mIsFavouriteResource;
    }

现在点击双击将项目的值设置为True

item.get(position).setmIsFavourite(true);

在您的第二个活动中,只需使用 getmIsFavourite()

检查此值

编辑:

使用此getmIsFavourite()

if(list.get(position).getmIsFavourite())
{
     //do want you want when its true
}else{
     //do want your code when its false.
}