Warlock
Warlock

Reputation: 45

Listview with different view in each row

I want to implement something like this list photo this list contains different controls in each row like radio button,spinner etc.

Upvotes: 0

Views: 34

Answers (2)

XxGoliathusxX
XxGoliathusxX

Reputation: 982

Create an adapter and in the getView() implement an algorithm that handles the right layout and content

Example

public class MyAdapter extends ArrayAdapter {

        private List<String> names;

        public SimpleTextAdapter(Context context, List<String> names) {
            super(context, R.layout.custom_row_simple_text, names);
            this.names = names;

        }

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

            LayoutInflater inflater = LayoutInflater.from(getContext());
            View customView = null;
            if(position == 0){
              customView = inflater.inflate(R.layout.layout_1, parent, false);
              //Get Views from layout_1
              TextView tv = (TextView) customView.findViewById(R.id.nameTv);
              tv.setText(names.get(position));
            }else if(){
              customView = inflater.inflate(R.layout.layout_2, parent, false);
              //Get Views of layout_2
            }else{
              customView = inflater.inflate(R.layout.layout_3, parent, false);
              //_______,,_________
            }
            return customView;
        }
    }

Upvotes: 0

Collins
Collins

Reputation: 145

in your custom listview adapter add override these methods

@Override
public int getViewTypeCount() {
    return super.getViewTypeCount();
}

and

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

replace return super.getViewTypeCount(); with the number of view that you intend to use, then in your getView method you can modify you view from

if(convertView == null)
   convertView = inflater.inflate(R.layout.row, null);

to:

if(convertView == null)
   {
  if(getItemViewType(position) == 0)
     convertView = inflater.inflate(R.layout.row2, null);
}else{
       convertView = inflater.inflate(R.layout.row2, null);}

Note: This question must have been answered and you are supposed to post the code you tried earlier.

Upvotes: 1

Related Questions