Question

I need to display a list of products (from SQLite) with name,price,description. The products are grouped in categories. I take the products from an SQLite table that has all the information inside. The table is called "preturi" and it has the fields "Categorie", "ProductName", "Price", "Description"

So what I want to obtain is an ExpandableListView like bellow (see the layout further bellow) :

"Category one"
   "Product1", 25.22, "FirstProduct"
   "Product2", 21.13, "SecondProduct"
   "Product3", 16.24, "ThirdProduct"
"Category two"
   "Product4", 5.72, "FourthProduct"
   ...
...

Until now, I managed to obtain and display a grouped list but only Category/ProductName. I need to be able to display the other two properties of the product (price and description). How should I change my code in order to send and inflate these 2 extra fields?

So far, I use the following code:

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>

and then

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>

So basically I want to populate the "lblPret" and "lblDescription" labels with data from my SQLiteTable

The layout of the activity (PreturiActivity) where I display the expandableList is:

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>

The Activity class code is as follows:

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());
    }
}

I am pretty sure I should replace "HashMap<String, List<String>> listDataChild;" and List<String> continut = new ArrayList<String>(); with something else where I can put more information. What would that be and how do I handle it? (I don't paste here the adapter code unless you need to see it, but it's pretty standard)

The core of the adapter that I use is this:

@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;
}

Please help

Thank you

Was it helpful?

Solution

Two changes are required to be done:

  1. Instead of preparing List<String>, you can prepare a list of objects, e.g. List<ProductClass>.
  2. You have to define custom adapter for your ExpandableListView.

Custom adapter:

Where in getChildView() of your custom adapter, you have to get and set details into your views:

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

}

Check more at: http://www.survivingwithandroid.com/2013/01/android-expandablelistview-baseexpandablelistadapter.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top