Use array adapter with more views in row in listview

后端 未结 4 781
广开言路
广开言路 2020-12-16 01:48

I have stumbled upon a problem I can\'t quite get my head around, so I was hoping perhaps someone here have had the same problem or knew a good way of solving the problem. <

4条回答
  •  感情败类
    2020-12-16 02:03

    To achieve this you have to build a custom adapter and inflate your custom row layout. Using ArrayAdapter won't work because

    By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

    So, your custom adapter class could be somthing like:

    public class CustomAdapter extends ArrayAdapter {
        private final Activity activity;
        private final List list;
    
        public CustomAdapter(Activity activity, ArrayList list) {
            this.activity = activity;
            this.list = list;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
            ViewHolder view;
    
            if(rowView == null)
            {
                // Get a new instance of the row layout view
                LayoutInflater inflater = activity.getLayoutInflater();
                rowView = inflater.inflate(R.layout.rowlayout, null);
    
                // Hold the view objects in an object, that way the don't need to be "re-  finded"
                view = new ViewHolder();
                view.retaurant_name= (TextView) rowView.findViewById(R.id.restaurantname);
                view.restaurant_address= (TextView) rowView.findViewById(R.id.textView1);
    
                rowView.setTag(view);
            } else {
                view = (ViewHolder) rowView.getTag();
            }
    
            /** Set data to your Views. */
            Restaurants item = list.get(position);
            view.retaurant_name.setText(item.getTickerSymbol());
            view.restaurant_address.setText(item.getQuote().toString());
    
            return rowView;
        }
    
        protected static class ViewHolder{
            protected TextView retaurant_name;
            protected TextView restaurant_address;
        }
    }
    

    And your Restaurant.java class could as simple as I describe below:

    public class Restaurants {
        private String name;
        private String address;
    
        public Restaurants(String name, String address) {
            this.name = name;
            this.address = address;
        }
        public void setName(String name) {
            this.name= name;
        }
        public String getName() {
            return name;
        }
        public void setAddress(String address) {
            this.address= address;
        }
        public String getAddress() {
            return address;
        }
    }
    

    Now, in you main activity just bind you list with some data, like;

    /** Declare and initialize list of Restaurants. */
    ArrayList list = new ArrayList();
    
    /** Add some restaurants to the list. */
    list.add(new Restaurant("name1", "address1"));
    list.add(new Restaurant("name2", "address2"));
    list.add(new Restaurant("name3", "address3"));
    list.add(new Restaurant("name4", "address4"));
    list.add(new Restaurant("name5", "address5"));
    list.add(new Restaurant("name6", "address6"));
    

    At this point you're able to set the custom adapter to your list

    ListView lv = (ListView) findViewById(R.id.mylist);
    
    CustomAdapter adapter = new CustomAdapter(YourMainActivityName.this, list);
    lv.setAdapter(adapter);
    

    This is all and it should work nicelly, but I strongly recommend you to google for some better alternatives to implement others Adapters.

提交回复
热议问题