我正在查看一家餐馆的列表视图,当我按一个项目时,我会进行另一项活动,其中有关于餐馆的详细信息,并且可以通过单击“保存”按钮将该餐馆存储为我的最爱。
我在我的KafDetails活动中使用“共享”偏好设置,以便始终在保存Restaturant时进行存储。然后在FavKafActivity中读取数据并从菜单图标显示收藏夹的列表视图。
问题是我的收藏夹列表视图仅存储了一个保存的餐厅。当我要保存另一个时,它永远不会显示在我的收藏夹列表视图中,只有第一个。
我在android方面并不陌生,不知道我在哪里做错了?如何读取为FavKaf保存的所有项目? (每当单击了保存该特定项目的按钮时)谢谢
KafDetails.java
final Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_zamenasave));
// kafani.get(pozicija).setState(true);
Integer favidk = idk;
String favimek = imek;
String favadresa = adresa;
String favtelbroj = telbroj;
String favemail = email;
String favwebstrana = webstrana;
String favslika = String.valueOf(0);
Double favlon = 0.0;
Double favlat = 0.0;
if (favkafani == null) {
favkafani = new ArrayList<>();
favkafani.add(new FavKaf(favidk, favimek, favadresa, favtelbroj, favemail, favwebstrana, favslika, favlon, favlat));
}
else {
favkafani.add(new FavKaf(favidk, favimek, favadresa, favtelbroj, favemail, favwebstrana, favslika, favlon, favlat));
}
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(favkafani);
editor.putString("task list", json);
editor.apply();
}
});
FavKafActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favkafani);
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<FavKaf>>() {}.getType();
favkafani = gson.fromJson(json, type);
editText = (EditText) findViewById(R.id.search);
listViewFav = (ListView) findViewById(R.id.flist);
adapter3 = new CustomAdapter3(favkafani, getApplicationContext());
listViewFav.setAdapter(adapter3);
listViewFav.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override...
和我的CustomAdapter3.java
public class CustomAdapter3 extends ArrayAdapter<FavKaf> implements
View.OnClickListener{
private ArrayList<FavKaf> favkafani;
Context mContext;
// View lookup cache
private static class ViewHolder
{
TextView imek;
TextView adresa;
ImageView image;
}
public CustomAdapter3(ArrayList<FavKaf> items, Context context) {
super(context, android.R.layout.activity_list_item, items);
this.favkafani = items;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
FavKaf favkafana = (FavKaf) object;
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
FavKaf favkafana = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder.imek = (TextView) convertView.findViewById(R.id.ime);
viewHolder.adresa = (TextView) convertView.findViewById(R.id.avtor);
viewHolder.image = (ImageView) convertView.findViewById(R.id.listview_image);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
//Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
//result.startAnimation(animation);
lastPosition = position;
viewHolder.imek.setText(favkafana.getImek());
viewHolder.adresa.setText(favkafana.getAdresa());
//viewHolder.info.setOnClickListener(this);
//viewHolder.info.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}