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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:02:48+00:00 2026-05-24T12:02:48+00:00

I’m inflating two layout in one listview. The data are in json format. What

  • 0

I’m inflating two layout in one listview. The data are in json format. What I do is I concat the json together, and check the appropriate position for each layout (the first layout only appear from position 0 to first-json’s length – 1).

The problem arise when the list view get longer, and I can scroll down through it. It came out with a Null Pointer exception. So I commented some of the code, and the error disappear. But what I got is not really what I’m expecting:
It interchange the format randomly. (Assume the length of the first json is x, so 0 to x-1 should be the first layout. But when I scroll up & down, sometimes somewhere between 0 and x changed to second layout. As well as other row > x, sometimes changed to first layout)

Here’s the code of the adapter

public class CustomAdapter extends BaseAdapter {

private Activity activity;
private JSONArray data;
private static LayoutInflater inflater=null;
private SecondItem second_item;
private FirstItem first_item;
private int firstLength;
private boolean hasFirst = false;

public CustomAdapter(Activity a, JSONArray firstArray, JSONArray secondArray) {
    activity = a;
    first_item = new FirstItem (firstArray);
    second_item = new SecondItem (secondArray);
    firstLength = first_item.getLength();
    if (!firstArray.isNull(0)) {
        data=CustomUtils.concatJsonArray(firstArray, secondArray);
        hasFirst = true;
    }
    else
        data = secondArray;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    // TODO Auto-generated method stub
    return data.length();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public static class ViewHolder{
    public TextView txt_both;
    public TextView txt_first_only;
    ...
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        if (position < firstLength && hasFirst) {
            vi = inflater.inflate(R.layout.first_item, null);
        }
        else {
            vi = inflater.inflate(R.layout.second_item, null);
        }


        holder=new ViewHolder();
        holder.txt_both=(TextView)vi.findViewById(R.id.txt_both);
        ...

        if (position < firstLength && hasFirst) {
            holder.txt_firstOnly=(TextView)vi.findViewById(R.id.txt_firstOnly);
            ...

        }
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    if (position < firstLength && hasFirst) {
            holder.txt_both.setText(first_item.getContent(position));
            ...

here is the commented block, which causes null pointer exception before (The error point the line holder.txt_firstOnly.setText) :

        /*if (first_item.getStatus(position)==0) {
            holder.txt_firstOnly.setText(...);              

        }
        else if (first_item.getStatus(position)==1) {
            holder.txt_firstOnly.setText(...);
        }
        else if (first_item.getStatus(position)==2) {
            holder.txt_firstOnly.setText(...);

        } */

and here’s the rest of the code :

        }
    else {
        holder.txt_both.setText(second_item.getContent(position - firstLength));
        ...

    }

    return vi;
}


}
  • 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-24T12:02:49+00:00Added an answer on May 24, 2026 at 12:02 pm

    Your custom adapter should override the methods getViewTypeCount() and getItemViewType (int position). Right now, the OS thinks all the views have the same view type, so it’s arbitrarily passing in a previously created view. As you’ve seen, sometimes this isn’t the right view for the position.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
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.