我正在尝试为我的应用添加书签,但我的代码中有一些费用,下面我将解释我的问题。
让我们从我的适配器memes.class
开始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;
}
}
主要活动
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
ArrayList<memes> item;
ArrayAdapter<memes> arrayAdapter;
String[] Nomes = new String[]{"Compartilhar", "Add Favoritos"};
List<memes> favoriteMemes = new ArrayList<>();
@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("item 1", R.raw.audio1));
item.add(new memes("item 2", R.raw.audio2));
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(arrayAdapter);
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("Menu");
builder.setItems(MainActivity.this.Nomes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
sendWhatsAppAudio(position);
return;
case 1:
//set item true
item.get(position).setmIsFavourite(true);
return;
default:
}
}
});
builder.show();
return true;
}
});
}
这是我将项目发送到其他活动的地方
//Here is the mess.
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 in my code it turns red
//what should I put after the item. to work?
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);
}
}
}
现在终于我们放置Favoritos.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);
}
}
此代码不显示项目。
我认为我的问题出在if (item.mIsFavorite ())
,但我不知道该放在这里。
还有一件事,在第二项活动中我将如何删除这些项目?
和tbm我可以推荐一个来自SharedPreferences的好教程,所以我可以使用那个案例。
谢谢