Android 如何使用多个HorizontalListView填充布局
我有一个水平的列表视图。它很好用。我的ArrayList列表将在屏幕上显示一个ListView。太好了。我的问题是eList有多行,这意味着eList可能是飞机、汽车和船只,或者是无限多的对象。目前,此水平列表视图将仅显示数据库中所有类型的飞机、汽车或船只。如何根据对象类型(飞机、汽车、船只、玉米饼、人)的多少,在屏幕上垂直显示多个或无限多个hListView 活跃Android 如何使用多个HorizontalListView填充布局,android,sqlite,listview,arraylist,Android,Sqlite,Listview,Arraylist,我有一个水平的列表视图。它很好用。我的ArrayList列表将在屏幕上显示一个ListView。太好了。我的问题是eList有多行,这意味着eList可能是飞机、汽车和船只,或者是无限多的对象。目前,此水平列表视图将仅显示数据库中所有类型的飞机、汽车或船只。如何根据对象类型(飞机、汽车、船只、玉米饼、人)的多少,在屏幕上垂直显示多个或无限多个hListView 活跃 HorizontalListView hListView = (HorizontalListView) findViewById(
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);
HorizontalListView hListView=(HorizontalListView)findViewById(R.id.hListView 1);
setAdapter(新的ItemAdapter());
ArrayList eList=controller.getAllts();
ListAdapter=newsimpledapter(Su.this、eList、R.layout.view、images、ins);
hListView.setAdapter(适配器);
在数据库中
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;
}
public ArrayList getAllts(){
ArrayList列表3;
List3=新的ArrayList();
String selectQuery3=“选择与INV不同的*,其中p2不为空,由p2 COLLATE NOCASE ASC排序”;
SQLiteDatabase=this.getWritableDatabase();
Cursor cursor3=database.rawQuery(selectQuery3,null);
HashMap=newHashMap();
if(cursor3.moveToFirst()){
做{
map.put(“iImageL”,cursor3.getString(13));
map.put(“p2”,cursor3.getString(2));
map.put(“se2”,cursor3.getString(10));
map.put(“te”,cursor3.getString(17));
列表3.添加(地图);
}while(cursor3.moveToNext());
}
close();
返回列表3;
}
在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>
我将假设不同类型的所有对象都在一个SQLite表中,类型作为字符串存储在其中一列中,并且每个类型没有不同的表 更改getAllts()函数以返回HashMaps的ArrayList的HashMap,而不是HashMaps的ArrayList,以便可以创建多个HorizontalListView,每个HashMaps的ArrayList对应一个视图,每个视图对应一种特定类型。在遍历游标中返回的行时,获取类型并检查大HashMap中是否已有该行的ArrayList,如果没有,则创建一个,如果有,则添加到现有的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;
}
public HashMap getAllts(){
HashMap HashMap=新的HashMap();
String selectQuery3=“选择与INV不同的*,其中p2不为空,由p2 COLLATE NOCASE ASC排序”;
SQLiteDatabase=this.getWritableDatabase();
Cursor cursor3=database.rawQuery(selectQuery3,null);
int-typeColumnIndex=18;//我的错误。我建议的编辑错误。我没有注意到这一行-->List3.add(map);
。与其使用HashMaps的ArrayList,不如创建一个自定义对象并使用对象的ArrayList?或者使用HashMaps的ArrayList有什么特别的原因吗?基本上我有人、地点和东西。我可以在屏幕上填充人,可以是蓝色的人、绿色的人等等ngs。我的问题是我需要一排人,然后在那下面一排地方,然后在那下面一排地方,依此类推。我发现我可能可以通过使用INV的idnumber,然后告诉活动根据int idnumber创建另一个HorizontalListView来实现这一点,但必须有更简单的方法。如果我理解正确,您当前的实现是一个水平滚动的listview,对吗?您希望该列表视图有3行,每个行水平滚动,对吗?不,我希望该HorizontalListView(HLV)有多个垂直实例基于列中的行数。列名称应为stuff 1)第一列行应为HLV people。。。。。。。。2)行将是HLV的位置3)行将是HLV的东西。对不起,我不理解你的描述。你能创建一个你当前拥有的和你想要的东西的图像吗?水平列表视图正在工作,除了它是在所有水平列表视图中显示相同的条目。日志显示正确的信息在LisfView中,但屏幕上显示的是所有条目的相同信息。我的意思是“John”在每个条目中都会显示,尽管日志显示的是John first Listview,然后是每个Listview上的正确汽车、城市等。我想我需要在SimpleAdapter中输入typename,或者将getAllts()更改为getAllts(typename),但如果这样做,我就无法获得entry.getKey,因为它不会在getAllts()中解析,尝试在循环内创建HashMap(请参见编辑)
<LinearLayout
android:id="@+id/LinearViewa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:orientation="vertical" >
</LinearLayout>
<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>
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);
}