0

I have a List<Service> received from getParcelableArrayListExtra() Now I need to populate the received objects into a android ListView

I have try to populate the ListView in the following way. The ListView is populated to show the Object reference ids. Instead of showing contants.

How do i show the contents in this list.

The implementation is as follow :

List<Service> serviceCart = getIntent().getParcelableArrayListExtra("serviceCart");

ListView listView = (ListView) findViewById(R.id.lv_choosed_services);

ArrayAdapter<Service> serviceArrayAdapter = new ArrayAdapter<Service>(this,android.R.layout.simple_list_item_1,serviceCart);

listView.setAdapter(serviceArrayAdapter);
nifCody
  • 2,394
  • 3
  • 34
  • 54

1 Answers1

1

Open ArrayAdapter and go to read from line no 405 to 410

final T item = getItem(position);
if (item instanceof CharSequence) {
    text.setText((CharSequence) item);
} else {
    text.setText(item.toString());
}

What you are doing:

If your item is String Adpater.getView(params) will use it, otherwise it will use item.toString(). In your Service.java you don't have a toString() so application uses default default toString()

What you should do:

  • Its best to create custom ArrayAdapter and populate your view
  • Override toString() in your Service.java and use only those values you want to populate in your adapter like service_name and/or service_code and default implementation will do the rest.

Note: Please read Naming Conventions, its not recommended to name your objects like service_name it should be serviceName.

Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28