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

  • SEARCH
  • Home
  • 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 6983619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:26:35+00:00 2026-05-27T18:26:35+00:00

I’m trying to create a custom cursoradapter that will use two different layouts depending

  • 0

I’m trying to create a custom cursoradapter that will use two different layouts depending on some data in the cursor. I keep reading about ‘overriding getViewTypeCount() and getItemViewType()’ to make this happen but I can’t seem to figure out how to implement this..

This is my code for the bindView and new View methods:

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tView = (TextView) view.findViewById(R.id.TextView1);
    tView.setText("The text");
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return mInflater.inflate(R.layout.item1, parent, false);
}

.

EDIT:
Now I got this working, but I want to choose what rowlayout to use depending on some data in the cursor and I can’t get that working.. anybody got some ideas?

@Override
public int getItemViewType(int position) {
    return position % 2;
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.txtAddress
            .setText("blabla");
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View v = null;
    int type = cursor.getPosition() % 2;
    if(type == 0) {
        v = mInflater.inflate(R.layout.item, parent, false); 
    } else {
        v = mInflater.inflate(R.layout.item2, parent, false);
    }

    holder.txtAddress = (TextView) v.findViewById(R.id.tvName);

    v.setTag(holder);
    return v;
}
  • 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-05-27T18:26:36+00:00Added an answer on May 27, 2026 at 6:26 pm

    So I finally got it work. For the ones interested the working code is below:

    private int getItemViewType(Cursor cursor) {
        String type = cursor.getString(cursor.getColumnIndex("type"));
        if (type.equals("1")) {
            return 0;
        } else {
            return 1;
        }
    }
    
    @Override
    public int getItemViewType(int position) {
        Cursor cursor = (Cursor) getItem(position);
        return getItemViewType(cursor);
    }
    
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.textView
                .setText(cursor.getString(cursor.getColumnIndex("body")));
    }
    
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        View v = null;
    
        if (cursor.getString(cursor.getColumnIndex("type")).equals("1")) {
            v = mInflater.inflate(R.layout.message1, parent, false);
            holder.textView = (TextView) v
                    .findViewById(R.id.textView1);
        } else {
            v = mInflater.inflate(R.layout.message2, parent, false);
            holder.textView = (TextView) v
                    .findViewById(R.id.textView2);
        }
    
        v.setTag(holder);
        return v;
    }
    
    public static class ViewHolder {
        public TextView textView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.