Custom List View duplitcating Items
-
07-03-2021 - |
Question
I am creating a custom ListView by extending the ArrayAdapter. When i visit the activity the first time it shows up correctly. when i go back and revisit the Activity. It repeats the content all over again at the end of the actual array. How can i deal with the repeating content ?
Custom Adapter
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;
}
}
Activity
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;
}
}
Solution
Modify your generateArray()
this way
public ArrayList<Item> generateArray(String JSONdata) throws JSONException {
ArrayList<Item> dataArray = new ArrayList<Item>();
//rest of your code..
return dataArray;
}
OTHER TIPS
The dataArray will grow on each onCreate
(which is called each time you show the view) because you are adding new items.
Just do a clear on dataArray on the begining of the onCreate
must fix your problems.
change your getView Method and it will work..I am posting the code......
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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow