Sumit M Asok Sumit M Asok - 1 year ago 97
Android Question

How to use ArrayAdapter<myClass>

ArrayList<MyClass> myList = new ArrayList<MyClass>();

ListView listView = (ListView) findViewById(;

ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this, R.layout.row,
to, myList.);

Class: MyClass

class MyClass {
public String reason;
public long long_val;

I have created row.xml in layouts, but don't know how to show both reason and long_val in the ListView using ArrayAdapter.

Answer Source

Implement custom adapter for your class:

public class MyClassAdapter extends ArrayAdapter<MyClass> {

    private static class ViewHolder {
        private TextView itemView;

    public MyClassAdapter(Context context, int textViewResourceId, ArrayList<MyClass> items) {
        super(context, textViewResourceId, items);

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext())
            .inflate(R.layout.listview_association, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.itemView = (TextView) convertView.findViewById(;

        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        MyClass item = getItem(position);
        if (item!= null) {
            // My layout has only one TextView
                // do whatever you want with your string and long
            viewHolder.itemView.setText(String.format("%s %d", item.reason, item.long_val));

        return convertView;

For those not very familiar with the Android framework, this is explained in better detail here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download