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 8734611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:52:39+00:00 2026-06-13T09:52:39+00:00

For each list view with difference row layout template, I must create each custom

  • 0

For each list view with difference row layout template, I must create each custom adapter, which do the same thing: load xml row layout, get control (TextView, ImageView, etc..) by id, display data… something like this:

public class CommentAdapter extends BaseAdapter {

protected Activity activity;
protected static LayoutInflater layoutInflater = null;  
protected List<Comment> lst;    

public CommentAdapter(Activity activity,  List<Comment> lst){
    this.activity = activity;       
    this.lst = lst;
    layoutInflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {     
    return lst.size();
}

public Object getItem(int position) {   
    return lst.get(position);
}

public long getItemId(int position) {   
    return position;
}

public static class ViewHolder{
    public TextView textName;
    public TextView textComment;
    public ImageView image;
}

public  View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder viewHolder;
    if (v == null) {
        v = layoutInflater.inflate(R.layout.listitem, null);
        viewHolder = new ViewHolder();
        viewHolder.textName = (TextView) v.findViewById(R.id.txtName);
        viewHolder.image = (ImageView) v.findViewById(R.id.icon);
        viewHolder.textComment = (TextView)v.findViewById(R.id.txtComment);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }
    Static.overrideFonts(v);
    viewHolder.image.setBackgroundResource(lst.get(position).Icon);
    viewHolder.textName.setText(lst.get(position).Name);
    viewHolder.textComment.setText(lst.get(position).Comment);

    return v;
}
}

With many kind of list view (difference row layout template), I have to create many adapters.
So, the question is that I want to create one template adapter, which can be dynamic load row xml, map view control base on its id (maybe use reflect). The row xml layout, the control id, view control will be defined in another places.
Is there any design pattern, example or framework can achieve this?

  • 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-13T09:52:40+00:00Added an answer on June 13, 2026 at 9:52 am

    Something like this seems doable. I haven’t tested this at all, but in theory you could get away with something like this:

    public interface Adaptable {
        //Interface that any object you make that should be put in a listview
        //should implement
        public View buildView(View v, LayoutInflater inflater);
        public int getLayoutId();
    }
    
    public class MyObject implements Adaptable
        //Just hardcode your layout for this type of object
        public int getLayoutId() {
            return R.layout.myLayout;
        }
    
        //getView() will pass the recycled view to this method
        //which will handle building the view per this object
        public View buildView(View v, LayoutInflater inflater) {
            if(v == null) {
                v = inflater.inflate(getLayoutId());
                //Other initialization
            } //other initialization
    
            return v;
        }
    }
    
    //Then make the generic adapter that handles anything that implements
    //the Adaptable interface
    public GenericAdapter implements ListAdapter {
        private LayoutInflater inflater;
        private List<Adaptable> items;
        public GenericAdapter(List<Adaptable> items, Context c) {
            this.items = items;
            inflater = LayoutInflater.from(c);
        }
    
        //Now, using polymorphism, it should return a correctly built
        //view for whatever object type you've passed in.
        @Override
        public View getView(int pos, View convertView, ViewGroup parent) {
            return items.get(pos).buildView(convertView, inflater);
        }
    }
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        List<MyObject> objects = new ArrayList<MyObject>();
        //Fill your list however.
    
        //get your ListView, then...
        listView.setAdapter(new GenericAdapter(object, this));
    }
    

    It’s possible I’ve totally overlooked some reason this wouldn’t work — I typed this up in five minutes. In theory I think it would work, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom list view, each row has it's own custom selector,
I am using a list view and an adapter for loading a list,each list
I have a list view with 10 columns and I want each row to
I have a list view that is built using a simpleAdapter, each row is
I have a list view with 2 buttons on each row. I am using
I'm creating a list view in Android where each row is shown with LinearLayout
I have implemented a list view. Each item in my list view has two
I have a list view that displays a collection of items, each item has
I have a list view with over 100 items. Each of these items show
I have a website that lets people view rows in a table (each row

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.