如何使用多个 Horizo​​ntalListView 填充布局

How to populate Layout with multiple HorizontalListView

我有一个水平列表视图。它工作正常。我的 ArrayList eList 将在屏幕上显示一个 ListView。那太棒了。我的问题是 eList 有多行,这意味着 eList 可能是飞机、汽车和船只,或者无限数量的对象。目前这个 Horizo​​ntal ListView 将只显示数据库中所有种类的飞机或汽车或船。如何根据多少对象类型(飞机、汽车、船、炸玉米饼、人)垂直向下显示多个或无限数量的 hListView。

在ACTIVITY

HorizontalListView hListView = (HorizontalListView) findViewById(R.id.hlistview1);
    hListView.setAdapter(new ItemAdapter());
    ArrayList<HashMap<String, String>> eList = controller.getAllts();
    ListAdapter adapter = new SimpleAdapter(Su.this,eList,R.layout.view_m_ts, images, ins);
hListView.setAdapter(adapter);

在数据库中

public ArrayList<HashMap<String, String>> getAllts() {
    ArrayList<HashMap<String, String>> List3;
    List3 = new ArrayList<HashMap<String, String>>();
    String selectQuery3 = "SELECT DISTINCT * FROM INV where p2 IS NOT NULL ORDER BY p2 COLLATE NOCASE ASC";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor3 = database.rawQuery(selectQuery3, null);
    HashMap<String, String> map = new HashMap<String, String>();
    if (cursor3.moveToFirst()) {
        do {

            map.put("iImageL", cursor3.getString(13));
            map.put("p2", cursor3.getString(2));
            map.put("se2", cursor3.getString(10));
            map.put("te", cursor3.getString(17));
            List3.add(map);
        } while (cursor3.moveToNext());
    }
    close();
    return List3;
}

在XML

 <LinearLayout
    android:id="@+id/LinearViewa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".1"
    android:orientation="vertical" >

   <com.devsmart.android.ui.HorizontalListView
       android:id="@+id/hlistview1"
       android:layout_width="fill_parent"
       android:layout_height="10dp"
       android:layout_weight=".1"
       android:background="#000000" />

</LinearLayout>

我假设所有 objects 不同类型都在一个 SQLite table 中,类型作为字符串存储在其中一列中,而你没有每种类型不同 table。

将您的 getAllts() 函数更改为 return HashMap 的 ArrayLists 的 HashMap 而不是 HashMaps 的 ArrayList,这样您就可以创建多个 Horizo​​ntalListViews,每个 ArrayList of HashMaps 一个,每个 HashMaps 一个特定的类型。当您遍历游标中的 return 行时,获取类型并检查您的大 HashMap 中是否已经有一个 ArrayList,如果没有,则创建一个,如果是,则添加到现有的:

public HashMap<String, ArrayList<HashMap<String, String>>> getAllts() {
    HashMap<String, ArrayList<HashMap<String, String>>> hashMap = new HashMap<String, ArrayList<HashMap<String, String>>>();                

    String selectQuery3 = "SELECT DISTINCT * FROM INV where p2 IS NOT NULL ORDER BY p2 COLLATE NOCASE ASC";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor3 = database.rawQuery(selectQuery3, null);

    int typeColumnIndex = 18; // <- You need to set the correct column here, possibly using cursor3.getColumnIndexOrThrow()

    if (cursor3.moveToFirst()) {
        do {
            // create this HashMap inside the loop because we need a new one each time:
            HashMap<String, String> map = new HashMap<String, String>();
            ArrayList<HashMap<String, String>> List3;

            // get the type of this entry as a string       
            String typename = cursor3.getString(typeColumnIndex);
            // check if there already is an ArrayList for this type:
            if(hashMap.containsKey(typename))
            {
                // get existing ArrayList for this type:
                List3 = hashMap.get(typename);
            }
            else
            {
                // create new ArrayList for this type:
                List3 = new ArrayList<HashMap<String, String>>();
                hashMap.put(typename, List3);
            }
            map.put("iImageL", cursor3.getString(13));
            map.put("p2", cursor3.getString(2));
            map.put("se2", cursor3.getString(10));
            map.put("te", cursor3.getString(17));
            List3.add(map);
        } while (cursor3.moveToNext());
    }
    close();
    return hashMap;
}

更改您的 XML 以便您的 activity 中有一个空的 LinearLayout XML:

<LinearLayout
    android:id="@+id/LinearViewa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".1"
    android:orientation="vertical" >

</LinearLayout>

并为水平列表视图创建一个单独的 XML,您可以创建多次并动态添加到您的 LinearLayout,在 horiz_listview.xml 或类似的东西中。您可以设置自己的自定义布局,类型名称为 TextView header 等:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<!-- Add type name here if you like-->

    <com.devsmart.android.ui.HorizontalListView
        android:id="@+id/hlistview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000" />

</LinearLayout>

然后在您的 activity 中,获取包含所有 ArrayList 的大量哈希图并遍历它,为每个 ArrayList 创建一个 Horizo​​ntalListView 并将其添加到 LinearLayout:

LinearLayout layout = (LinearLayout)findViewById(R.id.LinearViewa);
HashMap<String, ArrayList<HashMap<String, String>>> eHash = controller.getAllts();
LayoutInflater inflater = getLayoutInflater();
for(Map.Entry<String, ArrayList<HashMap<String, String>>> entry : eHash.entrySet())
{
    // get the type name and ArrayList of Hashmaps for this type:
    String typename = entry.getKey();
    ArrayList<HashMap<String, String>> eList = entry.getValue();

    // inflate a horizonal list view and add it to the layout:
    View view = inflater.inflate(R.layout.horiz_listview, null, false);
    layout.addView(view);

    // set up the horizontal list view like before:
    HorizontalListView hListView = (HorizontalListView) view.findViewById(R.id.hlistview1);
    ListAdapter adapter = new SimpleAdapter(Su.this,eList,R.layout.view_m_ts, images, ins);
    hListView.setAdapter(adapter);
}

如果您的 Horizo​​ntalListViews 多于您可以在屏幕上显示的数量,那么您可能需要将主 LinearLayout 包装在 ScrollView 中,但是请注意,您可能 运行 遇到麻烦,请参阅 this question.