2015-01-26 59 views
1

我有一个水平的Listview。它工作正常。我的ArrayList eList将显示一个ListView穿过屏幕。那很棒。我的问题是,eList有多行,这意味着eList可能是飞机,汽车和船只,或无限数量的对象。目前,这个水平ListView将只显示数据库中的所有类型的飞机或汽车或船只。根据多少个对象类型(飞机,汽车,船,炸玉米饼,人),如何显示多个或无数个hListViews垂直垂直显示屏幕。如何使用多个Horizo​​ntalListView填充布局

活性

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

+0

我的不好。我建议的编辑是错误的。我没有注意到这一行 - >'List3.add(map);'。为什么不创建自定义对象并使用对象的ArrayList,而不是使用HashMap的ArrayList?还是有什么特别的理由,你正在使用一个ArrayList的HashMaps? – iRuth 2015-01-27 15:30:18

+0

基本上我有人,地方和事物。我可以在屏幕上用可以是蓝色人,绿色人等等的地方或地点或事物填充屏幕。我的问题是我想要一排人,然后是一排地下,一排地下,等等。我发现我可以通过使用INV中的idnumber来做到这一点,然后告诉Activity在int idnumber上创建另一个Horizo​​ntalListView基础,但必须有更简单的方法。 – SmulianJulian 2015-01-27 15:58:41

+0

如果我正确理解你,你当前的实现是一个水平滚动的列表视图,是否正确?你想要那个列表视图有3行,每个行都水平滚动,是否正确? – iRuth 2015-01-27 16:38:27

回答

2

我要去假设不同类型的所有对象的AR e在一个SQLite表中,其中一个列中存储的类型为字符串,并且每个类型没有不同的表。

更改getAllts()函数以返回HashMap的ArrayLists的HashMap而不是HashMap的ArrayList,以便您可以创建多个Horizo​​ntalListView,每个HashMap的ArrayList对应一个特定类型的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让你在活动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> 

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

<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> 

然后在你的活动,得到了大量的哈希表包含所有的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在滚动型,但被警告,你可能会遇到与问题,请参阅this question

+0

水平ListView正在工作,但在所有水平一览表中显示相同的条目。 LOG显示正确的信息在LisfViews中,但屏幕上显示的是所有条目的相同信息。我的意思是“约翰”在每个条目中都显示,尽管LOG显示约翰第一个ListView,然后是每个ListView上的正确的汽车,城市等。我想我需要在我的SimpleAdapter中获得typename或将getAllts()更改为getAllts(typename),但是如果我这样做,我无法获取entry.getKey,因为它不会在getAllts()中解析 – SmulianJulian 2015-01-31 12:54:34

+0

,请尝试创建HashMap循环内的地图(见编辑) – samgak 2015-01-31 18:05:27

+0

这样做,它现在工作正常。谢谢。 – SmulianJulian 2015-01-31 22:18:50

相关问题