[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

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?
0
errang
Asked:
errang
  • 3
  • 3
1 Solution
 
Dejan PažinCommented:

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/
0
 
errangAuthor Commented:
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.
0
 
Dejan PažinCommented:

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

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
errangAuthor Commented:
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..
0
 
Dejan PažinCommented:

The example should work. Did you check the log for any errors.

If the example doesnt work there must be a reason for it and the best way to check it is to check the log output.
0
 
errangAuthor Commented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now