简体   繁体   中英

Custom three item ListView adapter

I have a ListView layout with three values per item (grade_list.xml), but I cannot get my adapter to set the third value because the HashMap will only take two Strings. How would I get around this?

grade_list.xml

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
<!-- First Line - Header Text -->
<TextView
    android:id="@android:id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:layout_marginTop="8dp"
    android:textStyle="bold"/>
<!-- Second Line - Description -->
<TextView
    android:id="@android:id/text2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/text1"
    android:layout_alignStart="@android:id/text1"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail" />
<!-- Third Line - Grade -->
<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/title"
    android:gravity="right"
    android:layout_marginTop="8dp"
    android:layout_marginRight="5dip"
    android:textSize="10dip"
    android:textColor="#10bcc9"
    android:textStyle="bold"/></TwoLineListItem>

My server responds with something along the lines of

Period 1 - Language Arts;Period 2 - Biology;Period 3 - Engineering~~DIVIDER~~A+;B-;A

in the format of

Class;Class;Class~~DIVIDER~~Grade;Grade;Grade

then the client puts that data into the ListView with this code

final String[] ServerResponse = input.split("~~DIVIDER~~");
final String[] Classes = ServerResponse[0].split(";");
final String[] Grades = ServerResponse[1].split(";");

ArrayList<HashMap<String, String>> item_list = new ArrayList<HashMap<String, String>>();

for (int i = 0; i < Classes.length; i++) {
    HashMap<String, String> item = new HashMap<String, String>();
    item.put("Class", Classes[i]);
    item.put("Grade", Grades[i]);

    item_list.add(item);
}

String[] list_input = new String[] { "Class", "Grade" };
// PUT THE STRING VALUES FROM list_input INTO THE XML LAYOUT VALUES
int[] layout_values = new int[] { android.R.id.text1, android.R.id.text2};

// LISTVIEW LAYOUT (XML FILE)
int list_layout = grade_list;

ListView gradeListView = (ListView) findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));

I'd like to split the

Period 1 - Class Name 

Into two separate values, the period would populate the first line, and the class name would populate the second line, split by the - character. However, the HashMap will only hold two values, instead of my desired three.

The Android documentation does not give much help into this, nor does Google. Thanks in advance.

It is good to remember that the HashMap links the data to the line ID (two items). There is a HashMap created for each list line. The SimpleAdapter does the same iteration as your code for loading the ListView from each HashMap.

It'll be something like:

//Setting the HashMaps

HashMap<String,String> item;
for(int i=0;i<Classes.length;i++){
    item = new HashMap<String,String>();
    item.put( "text1", Period[i]);
    item.put( "text2", Classes[i]);
    item.put( "text3", Grades[i]);
    list.add( item );
}

//...

//Setting the adapter
gradeListView.setAdapter(new new SimpleAdapter(this, item_list, R.layout.TwoLineListItem, new String[] { "text1","text2", "text3" }, new int[] {R.id.text1, R.id.text, R.id.text3}));

Though you should really rename your TwoLineListItem to ThreeLineListItem to avoid future confusions.

You can make your custom object for a list item

public class ListItem {
    public String text1;
    public String text1;
    public String text1;

    public ListItem(String text1, String text2, String text3) {
        this.text1 = text1;
        this.text2 = text2;
        this.text3 = text3;
    }
}

Then use ArrayList<ListItem> and a custom adapter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM