我需要显示一个产品清单(从SQLite)的名称,价格,描述。这些产品按类别分类。我从包含所有信息的SQLite表中获取产品。该表名为"preturi“,它的字段为"Categorie”、"ProductName“、"Price”、"Description“
因此,我想获得的是一个类似于ExpandableListView的bellow (请参阅下面的布局):
"Category one"
"Product1", 25.22, "FirstProduct"
"Product2", 21.13, "SecondProduct"
"Product3", 16.24, "ThirdProduct"
"Category two"
"Product4", 5.72, "FourthProduct"
...
...到目前为止,我成功地获得并显示了一个分组列表,但只显示了类别/产品名。我需要能够显示产品的其他两个属性(价格和说明)。为了发送和膨胀这两个额外的字段,我应该如何更改我的代码?
到目前为止,我使用了以下代码:
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">
<TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:text="Category here"
android:textColor="#f9f93d" />
</LinearLayout>然后
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="200dp"
android:layout_height="65dip"
android:orientation="vertical" >
<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:text="Product name"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<TextView
android:id="@+id/lblPret"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dip"
android:paddingTop="2dp"
android:text="Price"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="65dip"
android:orientation="vertical" >
<TextView
android:id="@+id/lblDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dip"
android:paddingTop="5dp"
android:text="Description"
android:paddingBottom="5dp"
android:paddingLeft="10dp" />
</LinearLayout>
</LinearLayout>所以基本上我想用来自我的lblPret的数据填充"lblDescription“和”SQLiteTable“标签
我显示PreturiActivity的活动( expandableList )的布局是:
activity_preturi.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="ro.mypack.myapppack.myapp.app.PreturiActivity">
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>活动类代码如下:
public class PreturiActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preturi);
expListView = (ExpandableListView) findViewById(R.id.lvExp); // getting the listview
prepareListData(); // preparing list data
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter); // setting list adapter
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
String catego = "";
Integer i = 0;
final SQLiteDatabase db = new myDbHelper(getApplicationContext()).getWritableDatabase();
Cursor cy = db.rawQuery("SELECT DISTINCT categorie FROM preturi group by categorie", null);
if (cy != null) {
if (cy.moveToFirst()) {
do {
catego = cy.getString(cy.getColumnIndex("categorie"));
listDataHeader.add(catego);
Cursor cz = db.rawQuery("SELECT * FROM preturi ", null);
if (cz != null) {
if (cz.moveToFirst()) {
List<String> continut = new ArrayList<String>();
do {
catego = cz.getString(cz.getColumnIndex("den_produs"));
myprice = cz.getString(cz.getColumnIndex("price"));
mydescr = cz.getString(cz.getColumnIndex("description"));
continut.add(catego); // here I should be able to add more than just "catego" (product name)
} while (cz.moveToNext());
listDataChild.put(listDataHeader.get(i), continut);
}
}
i++;
}while (cy.moveToNext());
}
}我非常肯定,我应该用其他东西代替"HashMap<String, List<String>> listDataChild;"和List<String> continut = new ArrayList<String>();,这样我就可以提供更多的信息。那会是什么,我该如何处理?(除非您需要查看适配器代码,否则我不会在这里粘贴适配器代码,但它非常标准)
我使用的适配器的核心是:
@Override
public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
TextView txtListChild_pret = (TextView) convertView.findViewById(R.id.lblPret);
txtListChild.setText(childText);
txtListChild_pret.setText(childText);
return convertView;
}请帮帮忙
谢谢
发布于 2014-04-18 11:38:53
需要进行两项修改:
List<String>,而是准备一个对象列表,例如List<ProductClass>。自定义适配器:
在自定义适配器的getChildView()中,必须在视图中获取和设置详细信息:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.child_item_layout, parent, false);
}
TextView itemName = (TextView) v.findViewById(R.id.itemName);
TextView itemDescr = (TextView) v.findViewById(R.id.itemDescr);
ProductClass objProduct = yourArrayList.get(groupPosition).getItemList().get(childPosition);
itemName.setText(objProduct.getName());
itemDescr.setText(objProduct.getDescr());
return v;
}查看更多信息,请访问:http://www.survivingwithandroid.com/2013/01/android-expandablelistview-baseexpandablelistadapter.html
https://stackoverflow.com/questions/23153209
复制相似问题