在共享首选项和arraylist中添加多个字段

时间:2015-09-15 08:06:26

标签: android arraylist sharedpreferences

我正在开发一个应用程序,我将以下列格式从服务器获取数据。

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

这里我有两个字段标题和描述。我想将它们保存在共享首选项中,并在列表视图中显示它。下面是我在共享偏好中添加数据的代码。

  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();

并检索它

    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);

服务适配器类

  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;
}

1 个答案:

答案 0 :(得分:1)

问题在于getView of Service Adapter,下面是更正后的代码

@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;
}