Reputation: 13
I would like to send selected item from listview to another listview with sharedprefences. I can save item but cannot see this item another listview. Activity class
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();
This is list class which should display items.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);
Finally,this is Adapter Class
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;
}
}
Upvotes: 0
Views: 72
Reputation: 191671
You passed in a List, but you never assigned it.
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
}
It's worth pointing out, though, that you could also not do the above and entirely remove list
, add()
, and getItem()
, getCount()
definitions, then it'll likely work.
Any usage of list.get(position)
should instead be getItem(position)
Upvotes: 1
Reputation: 1251
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();
Upvotes: 0
Reputation: 497
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);
Upvotes: 0