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

标签 android sqlite listview arraylist

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

enter image description here

最佳答案

我假设所有不同类型的对象都在一个 SQLite 表中,类型作为字符串存储在其中一列中,并且您没有为每种类型创建不同的表。

更改您的 getAllts() 函数以返回 HashMap 的 ArrayList 的 HashMap 而不是 HashMap 的 ArrayList,这样您就可以创建多个 Horizo​​ntalListView,每个 HashMap 的 ArrayList 一个,每个对应一种特定类型。当您遍历游标中返回的行时,获取类型并检查您的大 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 XML 中有一个空的 LinearLayout:

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

</LinearLayout>

并为水平 ListView 创建一个单独的 XML,您可以在 horiz_listview.xml 或类似文件中创建多次并动态添加到您的 LinearLayout。您可以使用类型名称设置自己的自定义布局作为 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 的大量 HashMap 并遍历它,为每个 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 .

关于android - 如何使用多个 Horizo​​ntalListView 填充布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28160969/

相关文章:

javascript - 如何修复 NPM 安装 SQLite 构建失败?

android - 设置可扩展列表的样式

java - 无法使用 asynctask 获取自定义列表适配器来填充 ListView

android - 我会因为看到真实广告(展示次数)而被 AdMob 禁止吗?

android - view.getDrawingCache() 仅在 Samsung Galaxy note 8.0 中返回 null?

python - 为什么这个 sqlite 查询中的变量后面有一个逗号?

python - 提高sqlite中自连接的性能

android - ListView SimpleCursorAdapter 更新

android - 在flutter应用程序中获得互联网许可

java - 使用 JAutodoc 自动刷新文件头