首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复杂布局的ExpandableListAdapter

复杂布局的ExpandableListAdapter
EN

Stack Overflow用户
提问于 2014-04-18 11:29:25
回答 1查看 238关注 0票数 0

我需要显示一个产品清单(从SQLite)的名称,价格,描述。这些产品按类别分类。我从包含所有信息的SQLite表中获取产品。该表名为"preturi“,它的字段为"Categorie”、"ProductName“、"Price”、"Description“

因此,我想获得的是一个类似于ExpandableListView的bellow (请参阅下面的布局):

代码语言:javascript
复制
"Category one"
   "Product1", 25.22, "FirstProduct"
   "Product2", 21.13, "SecondProduct"
   "Product3", 16.24, "ThirdProduct"
"Category two"
   "Product4", 5.72, "FourthProduct"
   ...
...

到目前为止,我成功地获得并显示了一个分组列表,但只显示了类别/产品名。我需要能够显示产品的其他两个属性(价格和说明)。为了发送和膨胀这两个额外的字段,我应该如何更改我的代码?

到目前为止,我使用了以下代码:

代码语言:javascript
复制
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>

然后

代码语言:javascript
复制
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 )的布局是:

代码语言:javascript
复制
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>

活动类代码如下:

代码语言:javascript
复制
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>();,这样我就可以提供更多的信息。那会是什么,我该如何处理?(除非您需要查看适配器代码,否则我不会在这里粘贴适配器代码,但它非常标准)

我使用的适配器的核心是:

代码语言:javascript
复制
@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;
}

请帮帮忙

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-18 11:38:53

需要进行两项修改:

  1. 您可以不准备List<String>,而是准备一个对象列表,例如List<ProductClass>
  2. 您必须为您的ExpandableListView定义自定义适配器。

自定义适配器:

在自定义适配器的getChildView()中,必须在视图中获取和设置详细信息:

代码语言:javascript
复制
@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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23153209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档