1

I am developing an application in which i am getting data from server in the following format.

 [{"title":"demo title","description":"demo description"}]

Here i have two fields title and description. i want to save these in shared preferences and and show it in list view. below is my code to add the data in shared preferences.

  JSONArray arr = new JSONArray(strServerResponse);
  JSONObject jsonObj = arr.getJSONObject(0);
  ArrayList<String> tii = new ArrayList<String>();
  ArrayList<String> tii1 = new ArrayList<String>();
  for (int i = 0; i < arr.length(); i++) {
           pojo = new Pojo();
           JSONObject jobj2 = arr.getJSONObject(i);
           String title = jobj2.optString("title");
           String desc = jobj2.optString("description");
           tii.add(title);
           tii1.add(desc);
  }

  List<String> listTemp = tii;
  List<String> listTemp1 = tii1;
  Set<String> temp = new HashSet<String>(listTemp);
  Set<String> temp1 = new HashSet<String>(listTemp1);
  SharedPreferences.Editor editor = getSharedPreferences("MyPref4", MODE_PRIVATE).edit();
  temp.addAll(listTemp);
  temp1.addAll(listTemp1);
  editor.putStringSet("title", temp);
  editor.putStringSet("description", temp1);
  editor.commit();

and to retrieve it

    SharedPreferences prefs = getSharedPreferences("MyPref4", MODE_PRIVATE);
    Set<String> set = prefs.getStringSet("title", null);
    servicces.clear();
    for (String p : set) {
         pojo = new Pojo();
         pojo.setServiceTitle(p);
         servicces.add(pojo);
    }

    Set<String> set1 = prefs.getStringSet("description", null);
    for (String p2 : set1) {
         pojo = new Pojo();
         pojo.setServiceDesc(p2);
         servicces.add(pojo);
    }
    servicesAdapter = new ServicesAdapter(ServicesActivity.this, servicces);
    listServ.setAdapter(servicesAdapter);

service adapter class

  public class ServicesAdapter extends BaseAdapter {
TextView servTitle, servDes;
Pojo pojo;
private Context activity1;
ArrayList<Pojo> data1;
private ArrayList<Pojo> arraylist1 = null;
public static LayoutInflater inflater;

public ServicesAdapter(Context ctx, ArrayList<Pojo> catt) {
    // TODO Auto-generated constructor stub
    activity1 = ctx;
    data1 = catt;
    inflater = (LayoutInflater) activity1
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.arraylist1 = new ArrayList<Pojo>();
    this.arraylist1.addAll(data1);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data1.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return data1.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;

    v = inflater.inflate(R.layout.services_item, parent, false);
    pojo = data1.get(position);
    servTitle = (TextView) v.findViewById(R.id.servTitle);
    servTitle.setText(pojo.getServiceDesc());

    servDes = (TextView) v.findViewById(R.id.servDesc);
    servDes.setText(pojo.getServiceDesc());

    return v;
}
7
  • what problem u are getting? Commented Sep 15, 2015 at 8:09
  • both the text fields shows description even the title one Commented Sep 15, 2015 at 8:13
  • Then problem is in ServicesAdapter class show class code Commented Sep 15, 2015 at 8:14
  • 1
    servTitle.setText(pojo.getServiceDesc()); you are setting description here instead servTitle.setText(pojo.getServiceTitle()); Commented Sep 15, 2015 at 8:16
  • 1
    Change servTitle.setText(pojo.getServiceDesc()); to servTitle.setText(pojo.getServiceTitle()); Commented Sep 15, 2015 at 8:18

1 Answer 1

1

Problem is in getView of Service Adapter, below is the corrected code

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    .....

    servTitle = (TextView) v.findViewById(R.id.servTitle);
    servTitle.setText(pojo.getServiceTitle());

    servDes = (TextView) v.findViewById(R.id.servDesc);
    servDes.setText(pojo.getServiceDesc());

    return v;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.