Link to home
Start Free TrialLog in
Avatar of errang
errangFlag for Afghanistan

asked on

How do you overload a ListView

Hey,

        I was trying to overload a ListView, to include an image downloaded from the net, and have some text next to it.

I've been trying to find a decent tutorial concerning this, but I've only found inconsistent bits and pieces, and I just ended up with a huge project filled with errors.

Could anyone help me out?
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image


This shouldnt be that difficult, since it is a very basic functionality. You just need to create custom adapter which extends ArrayAdapter. There you override the method getView and make it return a view that shows the picture and the text.

There are also planty of tutorials that will show you the complete process. Here is one:

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Avatar of errang

ASKER

I actually tried that site, but it doesn't explain anything about creating a custom adapter... it just has 2 java classes, 1 of which is very basic, and 2 other basic xml files.

There is a complete explanation of how to make a custom adapter including code for it. The class is named OrderAdapter and it extends ArrayAdapter.

This is the copied class from the above link:


private class OrderAdapter extends ArrayAdapter<Order> {

        private ArrayList<Order> items;

        public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.row, null);
                }
                Order o = items.get(position);
                if (o != null) {
                        TextView tt = (TextView) v.findViewById(R.id.toptext);
                        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                        if (tt != null) {
                              tt.setText("Name: "+o.getOrderName());                            }
                        if(bt != null){
                              bt.setText("Status: "+ o.getOrderStatus());
                        }
                }
                return v;
        }
}

Open in new window

Avatar of errang

ASKER

Yea, I saw that the final code had all the pieces together, but for some reason it doesn't show me the overloaded ListView, I even copy pasted the code so, there shouldn't be any reason why it doesn't work..
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

Oh... guess one thing I'm actually getting an error with is, this line:

setListAdapter(this.m_adapter);

The time when it actually ran, I scrolled over it, and Eclipse gave me a quick fix of creating the class... but there was nothing in it... shouldn't the ListAdapter do something when its set?

I don't see that being overloaded in this example.