简体   繁体   中英

Android: Multiple Column ListView from Single Data

I tried to make a ListView with multiple column from a single array of strings.

I have tried to search some references, but every questions asked out there is quite different with what I'm trying to do. Generally, they are trying to show different data in different column in a ListView.

While, what I want is Multiple Column from single data array. For example:

ListView

Column-1     Column-2

Data1        Data2

Data3        Data4

Data5        Data6

DataN        DataN+1

Something like this, and also with single scrollbar.

So, I wanted to see the data array splitted to two column at once.

This a sample using Arraylist Hashmap.

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

This how you add items in Arraylist Hashmap

HashMap<String, String> data = new HashMap<>();

data.put("column1", "column1 value");
data.put("column2", "column2 value");
data.put("column3", "column3 value");

DataList.add(data); //1 row of item added in DataList

This how you use the SimpleAdapter

SimpleAdapter Adapter = new SimpleAdapter(MainActivity.this, DataList,
                R.layout.YOUR_LISTVIEW_LAYOUT, new String[]{"column1", "column", "column3"},
                new int[]{R.id.column1_textview, R.id.column2_textview, R.id.column3_textview});
        YOUR_LISTVIEW.setAdapter(Adapter);

YOUR_LISTVIEW.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="15dp"
    >

    <TextView
        android:id="@+id/column1_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/column2_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/column3_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

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