我通过扩展ArrayAdapter创建自定义ListView。当我第一次正确显示活动时。当我回去重新访问活动时。它在实际数组的末尾重复重复内容。我该如何处理重复内容?
自定义适配器
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item_default, null);
}
Item p = items.get(position);
if (p != null) {
TextView list_title = (TextView) v.findViewById(R.id.list_title);
TextView list_description = (TextView) v
.findViewById(R.id.list_description);
TextView list_timestamp = (TextView) v
.findViewById(R.id.list_timestamp);
ImageView list_image = (ImageView) v.findViewById(R.id.list_image);
if (list_title != null) {
list_title.setText(p.getItemTitle());
}
if (list_description != null) {
list_description.setText(p.getItemDescription());
}
if (list_timestamp != null) {
list_timestamp.setText(p.getItemTimestamp());
}
if (list_image != null) {
Log.d("bMobile", "inside getView() image");
try {
URL imageURL = new URL(p.getItemImage());
HttpURLConnection con = (HttpURLConnection) imageURL
.openConnection();
InputStream inputStrem = con.getInputStream();
Bitmap image = BitmapFactory.decodeStream(inputStrem);
if (null != image)
list_image.setImageBitmap(image);
else
Log.d("bMobile", "Bitmap is Null");
} catch (Exception e) {
}
}
}
return v;
}
}
活动
public class MessagesActivity extends Activity {
ListView listview;
static ArrayList<Item> dataArray = new ArrayList<Item>();
static ArrayList<Item> contentArray = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messages);
setupViews();
try {
contentArray = generateArray(createJson());
} catch (JSONException e) {
e.printStackTrace();
}
listview.setAdapter(new ListAdapter(MessagesActivity.this,
R.layout.list_item_default, contentArray));
}
public void setupViews() {
listview = (ListView) findViewById(R.id.list_items);
((TextView) findViewById(R.id.title_text))
.setText(R.string.description_messages);
}
// It creates a JSON and returns JSON string
public String createJson() throws JSONException {
JSONArray itemArray = new JSONArray();
JSONObject itemObject1 = new JSONObject();
itemObject1.put("title", "Harsha MV");
itemObject1.put("timestamp", "2 hours");
itemObject1.put("description", "Bangalore, India");
itemObject1.put("display_photo", "http://i.imgur.com/enUZr.jpg");
JSONObject itemObject2 = new JSONObject();
itemObject2.put("title", "Avinash G");
itemObject2.put("timestamp", "4 days");
itemObject2.put("description", "Mysore, India");
itemObject2.put("display_photo", "http://noblevelop.com/wp-content/uploads/2011/06/profile.jpg");
JSONObject itemObject3 = new JSONObject();
itemObject3.put("title", "Jyosna Sahoo");
itemObject3.put("timestamp", "1 year");
itemObject3.put("description", "Rourkela, India");
itemObject3.put("display_photo", "http://noblevelop.com/wp-content/uploads/2011/06/profile.jpg");
itemArray.put(itemObject1);
itemArray.put(itemObject2);
itemArray.put(itemObject3);
return itemArray.toString();
}
public ArrayList<Item> generateArray(String JSONdata) throws JSONException {
JSONArray listData = new JSONArray(JSONdata);
for (int i = 0; i < listData.length(); i++) {
JSONObject listObject = listData.getJSONObject(i);
String item_title = listObject.getString("title");
String item_description = listObject.getString("description");
String item_timestamp = listObject.getString("timestamp");
String item_image = listObject.getString("display_photo");
Item ObjectItem = new Item(item_title, item_timestamp,
item_description, item_image);
dataArray.add(ObjectItem);
}
return dataArray;
}
}
答案 0 :(得分:1)
dataArray将在每个onCreate
(每次显示视图时调用)上增长,因为您要添加新项目。
只需在onCreate
开头就明确dataArray就可以解决问题。
答案 1 :(得分:1)
以这种方式修改generateArray()
public ArrayList<Item> generateArray(String JSONdata) throws JSONException {
ArrayList<Item> dataArray = new ArrayList<Item>();
//rest of your code..
return dataArray;
}
答案 2 :(得分:1)
更改你的getView方法,它会工作..我发布代码......
public View getView(int position, View convertView, ViewGroup parent) {
convertView=null;
View v;
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item_default, null);
Item p = items.get(position);
if (p != null) {
TextView list_title = (TextView) v.findViewById(R.id.list_title);
TextView list_description = (TextView) v
.findViewById(R.id.list_description);
TextView list_timestamp = (TextView) v
.findViewById(R.id.list_timestamp);
ImageView list_image = (ImageView) v.findViewById(R.id.list_image);
if (list_title != null) {
list_title.setText(p.getItemTitle());
}
if (list_description != null) {
list_description.setText(p.getItemDescription());
}
if (list_timestamp != null) {
list_timestamp.setText(p.getItemTimestamp());
}
if (list_image != null) {
Log.d("bMobile", "inside getView() image");
try {
URL imageURL = new URL(p.getItemImage());
HttpURLConnection con = (HttpURLConnection) imageURL
.openConnection();
InputStream inputStrem = con.getInputStream();
Bitmap image = BitmapFactory.decodeStream(inputStrem);
if (null != image)
list_image.setImageBitmap(image);
else
Log.d("bMobile", "Bitmap is Null");
} catch (Exception e) {
}
}
}
return v;
}