我想将listview中的所选项目发送到另一个包含sharedprefences的列表视图。我可以保存项目,但无法看到此项目的另一个列表视图。 活动类
TextView textUrun = (TextView) view.findViewById(R.id.textUrun);
TextView textFiyat = (TextView) view.findViewById(R.id.textFiyat);
String yemek = textUrun.getText().toString();
String fiyat = textFiyat.getText().toString();
DataProvider dataProvider = new DataProvider(yemek,fiyat);
list.add(dataProvider);
String jsonurun = gson.toJson(list);
SharedPreferences sharedPreferences = getSharedPreferences("yemekbilgi", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("yemek",jsonurun);
editor.apply();
Toast.makeText(getApplicationContext(),"Listeye Kaydedildi",Toast.LENGTH_LONG).show();
这是应该显示项目的列表类。 Listele1
SharedPreferences sharedPreferences = getSharedPreferences("urun1", Context.MODE_PRIVATE);
List<DataProvider> list;
String jsonurun = sharedPreferences.getString("liste1", "");
Gson gson = new Gson();
DataProvider[] providers = gson.fromJson(jsonurun, DataProvider[].class);
list = Arrays.asList(providers);
listDataAdapter = new ListDataAdapter(getApplicationContext(), list);
listView = (ListView) findViewById(R.id.liste1);
listView.setAdapter(listDataAdapter);
最后,这是适配器类
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(@NonNull Context context,List<DataProvider> list) {
super(context, R.layout.be_layout, list);
}
static class LayoutHandler{
TextView ad,fiyat;
}
@Override
public void add(@Nullable Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
LayoutHandler layoutHandler;
if (view == null){
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.child_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.ad = (TextView) view.findViewById(R.id.textUrun);
layoutHandler.fiyat = (TextView) view.findViewById(R.id.textFiyat);
view.setTag(layoutHandler);
}
else
layoutHandler = (LayoutHandler) view.getTag();
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.ad.setText(dataProvider.getAd());
layoutHandler.fiyat.setText(dataProvider.getFiyat());
return view;
}
}
答案 0 :(得分:1)
您传入了一个列表,但您从未分配过它。
IMAX_BITS()
值得指出的是,您可能也无法执行上述操作并完全删除public class ListDataAdapter extends ArrayAdapter<DataProvider> {
List<DataProvider> list; // don't assign here
public ListDataAdapter(@NonNull Context context, @LayoutRes int resource,List<DataProvider> list) {
super(context, resource, list);
this.list = list; // see here
}
,list
和add()
,getItem()
定义,然后才能执行此操作我可能会工作。
getCount()
的任何用法应改为list.get(position)
答案 1 :(得分:0)
list = new ArrayList<DataProvider>();
gson = new Gson();
SharedPreferences sharedPreferences = getSharedPreferences("yemekbilgi", Context.MODE_PRIVATE);
String yemek = sharedPreferences.getString("yemek","");
Type type = new TypeToken<List<DataProvider>>(){}.getType();
list = gson.fromJson(yemek,type);
listView = (ListView) findViewById(R.id.liste1);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.child_layout,list);
listView.setAdapter(listDataAdapter);
答案 2 :(得分:0)
list = new ArrayList<DataProvider>();
listView = (ListView) findViewById(R.id.liste1);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.child_layout,list);
gson = new Gson();
SharedPreferences sharedPreferences = getSharedPreferences("yemekbilgi", Context.MODE_PRIVATE);
String yemek = sharedPreferences.getString("yemek","");
Type type = new TypeToken<List<DataProvider>>(){}.getType();
list.addAll(gson.fromJson(yemek,type));
listView.setAdapter(listDataAdapter);
listDataAdapter.notitifyDataSetChanged();