我正在使用Gson和Json来保存用于显示包含图像和文本的帖子的对象。问题是保存的帖子不会显示在listView中。我成功保存了帖子但没有加载它们。
PlaceViewFragment中的- 应该显示帖子
@Override
public void onStop() {
super.onStop();
SharedPreferences prefs = activity.getSharedPreferences(desination, 0);
SharedPreferences.Editor prefsEditor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(contentList);
prefsEditor.putString(desination, json);
prefsEditor.commit();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_place_view, container, false);
activity = this.getActivity();
desination = getArguments().getString("destination");
/**
*Load contentItems using Json.
**/
SharedPreferences prefs = activity.getSharedPreferences(desination, 0);
/*
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.clear();
prefsEditor.commit();
*/
Gson gson = new Gson();
String json = prefs.getString(desination, null);
Map<String, ?> keys = prefs.getAll();
for (Map.Entry<String, ?> entry : keys.entrySet()) {
Log.d("map values", entry.getKey() + ": " +
entry.getValue().toString());
}
Type type = new TypeToken<ArrayList<ContentItem>>() {
}.getType();
contentList = gson.fromJson(json, type);
// Log.d("Contentlist 1", contentList.get(0).getImages().get(0).toString());
if (contentList == null) {
contentList = new ArrayList<ContentItem>();
}
listView = (ListView) view.findViewById(R.id.listView2);
// get data from the table by the ListAdapter
listView.setAdapter(myAdapter);
myAdapter = new MyAdapter(this.getActivity(), R.layout.view_layout, contentList);
MyAdapter
@Override
public int getCount() {
return items.size();
}
@Override
public ContentItem getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
v = mInflater.inflate(R.layout.view_layout, parent, false);
holder = new ViewHolder();
holder.date = (TextView) v.findViewById(R.id.dateview);
holder.cap = (TextView) v.findViewById(R.id.caption);
holder.tt1 = (TextView) v.findViewById(R.id.textview1);
holder.tt2 = (TextView) v.findViewById(R.id.textview2);
holder.tt3 = (TextView) v.findViewById(R.id.textview3);
holder.im1 = (ImageView) v.findViewById(R.id.image1);
holder.im2 = (ImageView) v.findViewById(R.id.image2);
holder.im3 = (ImageView) v.findViewById(R.id.image3);
v.setTag(holder);
} else {
v = convertView;
holder = (ViewHolder) v.getTag();
}
ContentItem p = getItem(position);
holder.date.setText(p.getDate());
holder.cap.setText(p.getCaption());
if (p.getTexts().size() > 0) {
holder.tt1.setText(p.getTexts().get(0));
holder.tt1.setVisibility(View.VISIBLE);
holder.tt2.setText(p.getTexts().get(1));
holder.tt2.setVisibility(View.VISIBLE);
holder.tt3.setText(p.getTexts().get(2));
holder.tt3.setVisibility(View.VISIBLE);
}
if (p.getImages().size() > 0) {
holder.im1.setImageBitmap(loadImageFromStorage(p.getImages().get(0)));
holder.im1.setVisibility(View.VISIBLE);
holder.im2.setImageBitmap(loadImageFromStorage(p.getImages().get(1)));
holder.im2.setVisibility(View.VISIBLE);
holder.im3.setImageBitmap(loadImageFromStorage(p.getImages().get(2)));
holder.im3.setVisibility(View.VISIBLE);
}
return v;
}
private class ViewHolder {
public ImageView im1, im2, im3;
public TextView date, cap, tt1, tt2, tt3;
}
private Bitmap loadImageFromStorage(String path) {
Bitmap b = null;
try {
File f = new File(path, "image.jpg");
b = BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return b;
}
}
我认为这个代码出了问题。
答案 0 :(得分:1)
你反转了方法调用,你有
listView.setAdapter(myAdapter);
myAdapter = new MyAdapter(this.getActivity(), R.layout.view_layout, contentList);
应该是
myAdapter = new MyAdapter(this.getActivity(), R.layout.view_layout, contentList);
listView.setAdapter(myAdapter);