Search code examples
androidlistviewlistadapter

Can I have separate ListAdapters for different TextView items in a ListView?


I'm new here and a fairly newbish developer overall. I have looked online for an answer to my problem but it seems that noone has had such requirement. Perhaps my approach is flawed. Please help me.

I want to have a ListView with multiple TextViews that are populated by three different sets of data. First TV is a title of the item, second a summary and third a reference. The only approach I can think of is three separate ListAdapters for the separate TVs but as you can already imagine, every next ListAdapter overwrites the previous one. It looks like only one ListView adapter can be attached to a ListView layout. Is this correct? Here are snippets of my code.

Test hardcoded data objects:

    private void createDummyResults() {

    ArrayList<SearchResult> searchResults = new ArrayList<SearchResult>();

// the searchResults array of SearchResults objects is populated here with random data, but I have removed this code for brevity

    String[] searchResultsTitles = new String[searchResults.size()];
    String[] searchResultsSummaries = new String[searchResults.size()];
    String[] searchResultsReferences = new String[searchResults.size()];

    for (int i = 0; i < searchResults.size(); i++) {
        searchResultsTitles[i] = searchResults.get(i).getTitle();
        searchResultsSummaries[i] = searchResults.get(i).getSummary();
        searchResultsReferences[i] = searchResults.get(i).getReference();
    }

    // this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, searchResultsList));
    ArrayAdapter<String> aa1 = new ArrayAdapter<String>(this, R.layout.results, R.id.titlePane, searchResultsTitles);
    ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this, R.layout.results, R.id.summaryPane, searchResultsSummaries);
    ArrayAdapter<String> aa3 = new ArrayAdapter<String>(this, R.layout.results, R.id.referencePane, searchResultsReferences);

    this.setListAdapter(aa1);
    this.setListAdapter(aa2);
    this.setListAdapter(aa3);

}

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    >
    <LinearLayout
        android:id="@+id/resultPane"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:clickable="true"
        android:focusable="true"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:src="@drawable/uk_statute_64"
            >
        </ImageView>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#FFF"
            >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
            <TextView
                android:id="@+id/titlePane"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFF"
                android:textColor="#000"
                android:textStyle="bold"
                android:singleLine="true"
                android:text="This is the Title field"
                >
            </TextView>
            <TextView
                android:id="@+id/summaryPane"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFF"
                android:textColor="#000"
                android:singleLine="false"
                android:maxLines="2"
                android:text="This is the Summary field; it needs to be two lines long."
                >
            </TextView>
            <TextView
                android:id="@+id/referencePane"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFF"
                android:textColor="#AAA"
                android:singleLine="true"
                android:text="This is the ref field, eg. 1989 c.46 s.17"
                >
            </TextView>
        </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Solution

  • Why don't you make a SearchResults class, something like this:

    private class SearchResults {
    
        private String title;
        private String summary;
        private String reference;
    
        public String getTitle() {
            return title;
        }
    
        public String getSummary() {
            return summary;
        }
    
        public String getReference() {
            return reference;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public void setSummary(String summary) {
            this.summary = summary;
        }
    
        public void setReference(String reference) {
            this.reference = reference;
        }
    }
    

    Then you can write a simple custom ListAdapter that will take in a list of SearchResults objects, then you can set each part of your list item by calling getTitle(), getSummary(), and getReference() on each item as you're setting the view.

    EDIT: You should also take your layout into the hierarchy viewer, it can be simplified quite a bit. For example, your three TextViews are set in a LinearLayout within a LinearLayout. The outer one is completely redundant.

    EDIT AGAIN: Wait...looking closer, you already have a SearchResult class. Why are you splitting them into separate arrays when they're already their own objects? Just use the link above to create a custom ListAdapter that takes in an array of your SearchResult objects, and inserts the necessary data into the corresponding TextView.