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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:08:31+00:00 2026-06-17T13:08:31+00:00

I’m trying to display a ListView with different types of rows. I have a

  • 0

I’m trying to display a ListView with different types of rows.
I have a Result abstract class inherited by Artist, Label, Release etc.
I’m getting results from a JSON response. Parsing is ok, my factory manages to create some Result objects properly.

Using these guides 1, 2, I ended up extending BaseAdapter.

public class SearchResultsActivity extends ListActivity {

    private static final String SEARCH_API_ENDPOINT = "http://xxxx.com/database/search?q=";

    SearchResultAdapter searchAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_list);
        searchAdapter = new SearchResultAdapter();
        // handleIntent is needed because android:launchMode="singleTop" mode uses onNewIntent
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY); // get user input from search widget 
            String[][] params = new String[][]{{"q", query}}; // prepare GET parameters
            HttpRequestHandler requestHandler = new HttpRequestHandler("GET");
            String result;

            requestHandler.setURL(HttpRequestHandler.API_URL, HttpRequestHandler.SEARCH_QUERY_ENDPOINT);
            requestHandler.addParameters(params);
            result = requestHandler.sendRequest();
            if (result != null && !result.isEmpty()) {
                try {
                    ArrayList<Result> resultList = new ResultFactory(new JSONObject(result)).getResults(); // JSON to Result objects
                    for (Result res: resultList) {
                        searchAdapter.addItem(res);
                    }
                    setListAdapter(searchAdapter);
                } catch (JSONException e) {
                    System.out.println("xxjsonexception");
                }
            }
        }
    }

    public class SearchResultAdapter extends BaseAdapter {

        private List<Result> searchResults = new ArrayList<Result>();
        private LayoutInflater mInflater;

        public SearchResultAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void addItem(Result result) {
            searchResults.add(result);
            notifyDataSetChanged();
        }

        @Override
        public Object getItem(int position) {
            return searchResults.get(position);
        }

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

        @Override
        public int getCount() {
            return searchResults.size();
        }

        @Override
        public int getViewTypeCount() {
            return Result.Type.values().length; // enum length
        }

        @Override
        public int getItemViewType(int position) {
            return searchResults.get(position).getType().ordinal();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Result tmp = searchResults.get(position); // only for testing
            System.out.println("xxItem type " + tmp.getType());
            View v = searchResults.get(position).getView(mInflater, convertView);
            if (v == null) {
                System.out.println("xxgetviewnull");
            } else {
                System.out.println("xxgetviewok");
            }
            return v;
        }
    }
}

Result.java :

public abstract class Result {
    // general search result class

    public enum Type {
        ARTIST,
        RELEASE,
        MASTER,
        LABEL,
        ERROR,
    }

    public static final String NODE_RESULTS = "results";
    public static final String NODE_TITLE = "title";
    public static final String NODE_THUMB = "thumb";
    public static final String NODE_RESSOURCE = "resource_url";
    public static final String NODE_TYPE = "type";
    public static final String NODE_URI = "uri";
    public static final String NODE_ID = "id";

    public static final String TYPE_ARTIST = "artist";
    public static final String TYPE_RELEASE = "release";
    public static final String TYPE_MASTER = "master";
    public static final String TYPE_LABEL = "label";


    protected String thumb;
    protected String title;
    protected String ressource_url;
    protected static Type type;
    protected String uri;
    protected int id;


    public void setData(JSONObject jsonResult) {
        try {
            setId(jsonResult.getInt(Result.NODE_ID));
            setUri(jsonResult.getString(Result.NODE_URI));
            setRessource_url(jsonResult.getString(Result.NODE_RESSOURCE));
            setThumb(jsonResult.getString(Result.NODE_THUMB));
            setTitle(jsonResult.getString(Result.NODE_TITLE));
        } catch (JSONException e) {
            type = Type.ERROR;
        }
    }

    // getters and setters ..

    public abstract View getView(LayoutInflater inflater, View convertView);
}

Artist.java :

public class Artist extends Result{

    public Artist(){
        type = Type.ARTIST;
    }

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_artist, null);
        }
        TextView textTitle = (TextView) convertView.findViewById(R.id.title_artist);
        if (textTitle == null) {
            System.out.println("xxtexttitle null");
        } else {
            System.out.println("xxtexttitleok " + title);
        }
        textTitle.setText(title);
        return convertView;
    }

}

Label.java

public class Label extends Result {

    public Label() {
        type = Type.LABEL;
    }

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_label, null);
        }
        if (convertView == null) {
            System.out.println("xxconvertviewisnull");
        } else {
            System.out.println("xxconvertviewok");
        }
        TextView titleText = (TextView) convertView.findViewById(R.id.title_label);
        titleText.setText(title);
        return convertView;
    }

}

As you can see, my Artist and Label class are very similar but only my Label class throws a NPE at TextView titleText = (TextView) convertView.findViewById(R.id.title_label).

This is not a setContentView() as I am doing it in onCreate.
My Result objects are never null nor do they have null fields.
I do check to see if convertView is null in my getView methods, inflating a View if ever it is null.
I am not using a ViewHolder because Release and Master will be displaying extra fields.

EDIT :

list_item_label.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView android:id="@+id/title_label"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"/>

</LinearLayout>

list_item_artist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView android:id="@+id/title_artist"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textColor="#FFAAAA"
              android:textSize="14pt"/>

</LinearLayout>
  • 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-17T13:08:32+00:00Added an answer on June 17, 2026 at 1:08 pm

    It’s because your convertView is a “list_item_artist” view when you reach a Label object in your list. Then convertview isnt null, but the wrong type of view which cant find the title_label.

    Reverse the list order and you get a null pointer with a artist view instead.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
this is what i have right now Drawing an RSS feed into the php,

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.