Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:03:14+00:00 2026-06-17T10:03:14+00:00

I want to fill one single listview with different XML templates on different rows.

  • 0

I want to fill one single listview with different XML templates on different rows. I lost a tutorial and now I’m stuck. I get the same template for all the rows. So what I did, is, I created an Adapter and a Wrapper:

ListViewAdapter:

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class ListViewAdapter extends ArrayAdapter<ListItem> {



    private Activity activity = null;

    private List<ListItem> items = null;

    private int templateId = 0;

    public static final int TPL_TITLE = 1;

    public static final int TPL_PAYMENT = 2;

    public static final int TPL_NOTE = 3;



    public int getTemplateId() {

        return templateId;

    }



    public Activity getActivity() {

        return activity;

    }



    public void setActivity(Activity activity) {

        this.activity = activity;

    }



    public List<ListItem> getItems() {

        return items;

    }



    public void setItems(List<ListItem> items) {

        this.items = items;

    }



    public ListViewAdapter(Activity activity, List<ListItem> items,

            int templateId) {

        super(activity, android.R.layout.simple_list_item_1, items);

        this.items = items;

       this.activity = activity;

        this.templateId = templateId;

    }



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

        View row = convertView;

        ListViewWrapper wrapper = null;

        if (row == null) {

            LayoutInflater inflater = activity.getLayoutInflater();

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

            wrapper = new ListViewWrapper(row);

            row.setTag(wrapper);

        } else {

            wrapper = (ListViewWrapper) row.getTag();
        }

        wrapper.setTemplateId(templateId);

        wrapper.populateFrom(items.get(position));

        return (row);

    }



}

ListViewWrapper:

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ListViewWrapper {

    private TextView title = null; //txt

    private TextView description = null; //subtext

    private TextView amount = null;

    private TextView date = null; 

    private ImageView vink = null; 

    private ImageView label = null; //icon

    private TableRow listitem = null;

    private View row = null;

    private int templateId = 0;



    //Set the right template view to be visible

    private void setTemplate() {

        row.findViewById(R.id.templatetitle).setVisibility(View.GONE);

        row.findViewById(R.id.templatepayment).setVisibility(View.GONE);

        row.findViewById(R.id.templatenote).setVisibility(View.GONE);


        switch (templateId) {

        case ListViewAdapter.TPL_TITLE:

            row.findViewById(R.id.templatetitle).setVisibility(View.VISIBLE);

            break;

        case ListViewAdapter.TPL_PAYMENT:

            row.findViewById(R.id.templatepayment).setVisibility(View.VISIBLE);

            break;

        case ListViewAdapter.TPL_NOTE:

            row.findViewById(R.id.templatenote).setVisibility(View.VISIBLE);

            break;

        }

    }



    public ListViewWrapper(View row) {

        this.row = row;

    }



    //Fill the template with the right values

    public void populateFrom(ListItem r) {

        getText().setText(r.getTitle());


        if (templateId == ListViewAdapter.TPL_TITLE) {
             //Do Nothing... Title is enough


//            getIcon().setImageResource(

  //                  ImageUtilities.getDrawableById(r.getType()));

        }

        if (templateId == ListViewAdapter.TPL_PAYMENT) {
            //SUBTXT, AMOUNT, DATE

             getSubtext().setText(r.getDescription());
             getAmount().setText(r.getAmount());
             getDate().setText(r.getDate());
                     }

        if (templateId == ListViewAdapter.TPL_NOTE) {
               getLabel().setImageResource(r.getLabel());
               getBackground().setBackgroundResource(r.getBackground());
            if (r.getVinkOn()==1){getVink().setImageResource(R.drawable.vink_on);}else{getVink().setImageResource(R.drawable.vink_off);}
            }

        setTemplate();

    }



    public int getTemplateId() {

        return templateId;

    }



    public void setTemplateId(int templateId) {

        this.templateId = templateId;

        switch (templateId) {

        case ListViewAdapter.TPL_TITLE:

            break;

        case ListViewAdapter.TPL_PAYMENT:

            break;

        case ListViewAdapter.TPL_NOTE:

            break;

        }

    }


//SET MAIN TITLE ID
    TextView getText() {

        if (title == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_TITLE:

                title = (TextView) row.findViewById(R.id.titletitle);

                break;

            case ListViewAdapter.TPL_PAYMENT:

             title = (TextView) row.findViewById(R.id.titlepayment);

                break;

            case ListViewAdapter.TPL_NOTE:

                title = (TextView) row.findViewById(R.id.notetxt);

                break;

            }

        }

        return (title);

    }



    //Alternative text (all except template 2)

    TextView getSubtext() {

        if (description == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_PAYMENT:

                description = (TextView) row.findViewById(R.id.descriptionpayment);

                break;

            }

        }

        return (description);

    }


    TextView getAmount() {

        if (amount == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_PAYMENT:

                amount = (TextView) row.findViewById(R.id.groupamount);

                break;

            }

        }

        return (amount);

    }

    TextView getDate() {

        if (date == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_PAYMENT:

                date = (TextView) row.findViewById(R.id.date_payment);

                break;

        }


        }

        return (date);

    }



    ImageView getLabel() {

          if (label == null) {

              switch (templateId) {

          case ListViewAdapter.TPL_PAYMENT:

                  label = (ImageView) row.findViewById(R.id.labelpayment);

                  break;

          case ListViewAdapter.TPL_NOTE:

              label = (ImageView) row.findViewById(R.id.label_note);

              break;

      }
          }

        return (label);

    }

    ImageView getVink() {

      if (vink == null) {

            switch (templateId) {


        case ListViewAdapter.TPL_NOTE:

            vink = (ImageView) row.findViewById(R.id.vink);

            break;

    }
        }

      return (vink);

  }

    LinearLayout getBackground(){

      if (listitem == null) {

          switch (templateId) {

        case ListViewAdapter.TPL_PAYMENT:

              listitem = (TableRow) row.findViewById(R.id.templatepayment);

              break;

        case ListViewAdapter.TPL_NOTE:

            listitem = (TableRow) row.findViewById(R.id.templatenote);

            break;

        case ListViewAdapter.TPL_TITLE:

            listitem = (TableRow) row.findViewById(R.id.templatetitle);

            break;

  }
      }

    return (listitem);

    }

}

And created a ListItem Class (do I need to do this, or can I work with the Note.class and Payment.class?)

public class ListItem {

    private long id;
    private String title;
    private String description;
    private String amount;
    private String date;

    private int label;
    private int background;

    private int type;
    private int vinkon;


    public ListItem(long id, String title, String description, String amount, String date, int type, int vinkon){

        this.id=id;

        this.title = title;

        this.description = description;

        this.amount=amount;

        this.date=date;

        this.type = type;

        this.label=getLabel();

        this.background=getBackground();

        this.vinkon=vinkon;

    }

    public long getId() {
        return id;
      }

      public void setId(long id) {
        this.id = id;
      }


     public String getTitle() {
        return title;
      }

      public void setTitle(String title) {
        this.title = title;
      }

      public String getDescription() {
            return description;
          }

      public void setDescription(String description) {
            this.description = description;
          }

      public String getAmount() {
            return amount;
          }

      public void setAmount(String amount) {
            this.amount = amount;
          }
      public String getDate() {
            return date;
          } 

      public void setDate(String date) {
            this.date = date;
          }

      public int getType() {
                return type;
                      }

public void setType(int type) {
                this.type = type;
                      }

public int getVinkOn() {
    return vinkon;
          }

public void setVinkOn(int vinkon) {
    this.vinkon = vinkon;
          }

public int getLabel() {
    label=R.drawable.c_yellow;
    if (type==11){label=R.drawable.c_azul;}
    if (type==12){label=R.drawable.c_blue;}
    if (type==13){label=R.drawable.c_green;}
    if (type==14){label=R.drawable.c_orange;}
    if (type==15){label=R.drawable.c_purple;}
    if (type==16){label=R.drawable.c_red;}
    if (type==17){label=R.drawable.c_yellow;}
    return label;
          }

public void setLabel(int label) {
    this.label = label;
          }

public int getBackground() {
    background=R.drawable.c_yellow_bg;
    if (type==11){background=R.drawable.c_azul_bg;}
    if (type==12){background=R.drawable.c_blue_bg;}
    if (type==13){background=R.drawable.c_green_bg;}
    if (type==14){background=R.drawable.c_orange_bg;}
    if (type==15){background=R.drawable.c_purple_bg;}
    if (type==16){background=R.drawable.c_red_bg;}
    if (type==17){background=R.drawable.c_yellow_bg;}
    return background;
          }

public void setBackground(int background) {
    this.background = background;
          }


}

In my Activity, I have the following:

public void setList(){
         items = new ArrayList();


                title=getString(R.string.allpayments);
                items.add(new ListItem(id, title, description, amount, date, type, vinkon));    

            List<Payment> allpayments=paymentdatasource.getAllPayments();

                for (int i = 0;i<allpayments.size(); i++) {
                    Payment listpayment = allpayments.get(i);
                    id=listpayment.getId();

                 title=listpayment.getTitle();                     
                     description=listpayment.getDescription();
                     amount=listpayment.getGroupAmount();
                     date=listpayment.getDate();

                     items.add(new ListItem(id, title, description, amount, date, type, vinkon));

                }



        //Set adapter basing on template 1

        adapter = new ListViewAdapter(this, items, ListViewAdapter.TPL_PAYMENT);


        ListView mainMenu = (ListView) findViewById(R.id.listview);

        mainMenu.setAdapter(adapter);


        mainMenu.setTextFilterEnabled(true);

    }

Sorry, I lost the tutorial and now I have no clue how to get the different row layouts into the listview, hope it’s understandable and someone can help me a step further. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:03:15+00:00Added an answer on June 17, 2026 at 10:03 am

    Create a custom list adapter by creating a subclass of BaseAdapter(tutorial).
    In getView() method of your list adapter, inflate the view on the basis of you choice. I have used simple switch, you can you any logic to choose you layout.

        public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        TextView textView;
        View rv = null;
        if (convertView == null) { // if it's not recycled, initialize some
                                                    Display display = getWindowManager().getDefaultDisplay();
    
                rv = convertView;
    
                if (rv == null) {
    
            // Inflate from XML
    LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                    switch(id)
                    {
                    case 1:                 rv = li.inflate(R.layout.listitemlayout1, null);
                        break;
                    case 2:                 rv = li.inflate(R.layout.listitemlayout2, null);
    
                    break;
                    case 3:                 rv = li.inflate(R.layout.listitemlayout3, null);
    
                    break;
                    }
    
    }
    
    
    
    
            } else {
                rv = convertView;
            }
    
            return rv;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fill a ListView without an array of all the same things.
I want to fill all cells with the same control. What I have now
I have one table and I want to select 10 rows randomly. But I
I have a ListView , which is in single-choice mode . All I want
I want to fill items in a combobox, each of them has different behaviour.
I have a Spinner and I want to fill this one with a currency
I want to fill an array in javascript that takes all the values out
I want to fill jqGrid from a stored procedure, there was 1 good example
I simply want to fill-up cells in my spreadsheet from a VBA function. By
I have an ImageButton that I want to fill its parent RelativeLayout container for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.