Android Forum » Android Developer Forum » Android Developer Forum » Problem displaying values in List view

Problem displaying values in List view

Problem displaying values in List view
created on Jan 16, 2012 7:20:18 PM
Hi Guys,
I have a strange problem that I can't seem to find the answer to.

I have an Android Activity called MyActivitiesActivity that contains a ListView. The ListView is populated with class objects called PagedActivities which are essentially events from that are stored in my database.

These events are passed into my program through a website/web-service as a JSON string and then converted into an array of PagedActivities which is then used to create an ArrayAdapter for the ListView on the Android Activity.

The following Code Snippet shows the method that populates my code:

[code]

private void populateMyActivitiesList(JSONObject pagedActivitiesData) {
try {
// get reference to list view control
ListView myActivitiesList = (ListView) findViewById(R.id.listViewMyActivities);

if (pagedActivitiesData.getString("state").equals("1")) {

JSONArray jsonActivities = pagedActivitiesData.getJSONArray("activities");
// create array of paged activity objects to put the activities into
PagedActivity[] pagedActivities = new PagedActivity[jsonActivities.length()];
// for each activity found
for (int i = 0; i < jsonActivities.length(); i++) {
// add the activity to the array
JSONObject jPagedActivity = jsonActivities.getJSONObject(i);
PagedActivity activity = new PagedActivity(
jPagedActivity.getString("activityid"),
jPagedActivity.getString("startdate"),
jPagedActivity.getString("customername"),
jPagedActivity.getString("activitytype"));
pagedActivities[i] = activity;
}

// create an array adapter with the activities data array
ArrayAdapter<PagedActivity> pagedActivityAdapter = new ArrayAdapter<PagedActivity>(this, android.R.layout.simple_list_item_1, pagedActivities);

// assign the my activities list control the array adapter
myActivitiesList.setAdapter(pagedActivityAdapter);
Log.i(TAG, "Activities Loaded");

}
} catch (JSONException e) {
// log exception
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
}

[/code]

This causes the following output:



Rather than display my value it displays the project source file name and a reference to the object.

Now I have similar code for My Contacts which is working the same way but it displays correctly a display string instead as can be seen below

[code]

private void populateMyContactsList(JSONObject pagedContactsData) {
try {
// get reference to list view control
ListView myContactsList = (ListView)findViewById(R.id.listViewMyContacts);

if(pagedContactsData.getString("state").equals("1")) {

JSONArray jsonContacts = pagedContactsData.getJSONArray("contacts");
// create array of paged contact objects to put the contact into
PagedContact[] pagedContacts = new PagedContact[jsonContacts.length()];
// for each contact found
for(int i=0; i< jsonContacts.length(); i++)
{
// add the contact to the array
JSONObject jPagedContact = jsonContacts.getJSONObject(i);
PagedContact contact = new PagedContact(jPagedContact.getString("contactid"),
jPagedContact.getString("customerid"),
jPagedContact.getString("contactsearch"),
jPagedContact.getString("customernamenumber"));
pagedContacts[i] = contact;
}

// create an array adapter with the contact type data array
ArrayAdapter<PagedContact> pagedContactAdapter = new ArrayAdapter<PagedContact>(this, android.R.layout.simple_list_item_1, pagedContacts);

// assign the my contacts list control the array adapter
myContactsList.setAdapter(pagedContactAdapter);
Log.i(TAG, "Contacts Loaded");

}
} catch (JSONException e) {
// log exception
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
}

[/code]



Could someone please enlighten me why identical code is working differently on each activity?

Regards,

Iain
Reply with quote Reply Link ±0     (0 votes)
RE: Problem displaying values in List view
created on Jan 17, 2012 12:07:23 PM
Hi Guys,

I found the answer to my question ofter looking through my code.

I had not implemented the toString() method in my class PagedActivities which I had implemented in the PagedContacts class

Regards,

Iain
Reply with quote Reply Link ±0     (0 votes)