0

I have a working expandablelistview in my Android project, where each child contains an ImageView, a TextView, an EditText, and a Checkbox:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#CCCCCC"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgname"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="True"
    android:contentDescription="@+id/imgdesc"
    android:paddingLeft="1dp"
    android:scaleType="fitCenter"
    android:src="@drawable/logo_grey" />

<TableLayout
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toLeftOf="@+id/check1"
    android:layout_toRightOf="@+id/imgname"
    android:stretchColumns="0" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/desc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:textSize="13dp"
            android:background="#CCCCCC"
            android:textColor="#777777"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="fill_horizontal" >

        <EditText
            android:id="@+id/content"
            style="textViewStyle"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fadingEdge="none"
            android:focusable="true"
            android:hint="@+id/contenthint"
            android:isScrollContainer="true"
            android:minWidth="150dp"
            android:singleLine="false"
            android:textSize="10dp"
            android:textStyle="italic" >
        </EditText>
    </TableRow>
</TableLayout>

<CheckBox
    android:id="@+id/check1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:focusable="false"
    android:gravity="right" />
 </RelativeLayout>

For the purposes of writing to my SQLite database, I want to create a list which, for each child where the checkbox's value is true, contains the text from the TextView desc and the text from the EditText content, perhaps as an array of value pairs. The operation would be triggered by the user via a Button on the ExpandableListActivity. But I'm not sure how to identify the values of the widgets after inflating the view. I'd appreciate a little heads up, I've not been able to find a good answer online!

1 Answers1

0

I'm not exactly a professional myself, but i'll give it my best shot. first it'd help if you had something to keep track of checkbox states in your expandablelistview. If you don't already have one. Here's what i use, which i adapted from Android ExpandableListView with Checkbox, Controlling checked state:

//  set checkbox states
ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>(children.length);
public void setChildrenAndValues() { 
    for(int i = 0; i < children.length; i++) {
        ArrayList<Integer> tmp = new ArrayList<Integer>(children[i].length);
        for(int j = 0; j < children[i].length; j++) {
            tmp.add(1);
        }
        check_states.add(tmp);
    }
}

then in get childview:

    childcb.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            if (childcb.isChecked()) {
                check_states.get(groupPosition).set(childPosition, 1);
            }else{ check_states.get(groupPosition).set(childPosition, 0);
            }
        }
    });

Thus following along the same vein, to save your content for your database, you might try this on some button's void onclick method:

    if (check_states.get(groupPosition).get(childPosition) == 1) {
        desc_content4db.put(desc_tv.getText().toString(), content_et.getText().toString());
    }else{
        // placeholder code, w.e. you want
    }

and this else where:

HashMap<String,String> desc_content4db = new HashMap<String, String>();

I have no idea if desc_tv/content_et.getText().toString() would work like you want it to. what's likely and seemingly more clumsy is that you might already need to have an ArrayList of Hashmaps of all the content ready - and then copy over all the stuff that you want to put in the database depending on if it corresponds to the checkbox at which ever position checked. It doesn't sound pretty but it might work.

More ideal i would guess if you could step through the check_states list and depending on it it's 1 or 0, copy the edit_text and text_view at that id or position. sadly that's above my level right now. if you can do that, then more power to you.

Community
  • 1
  • 1
mango
  • 5,577
  • 4
  • 29
  • 41