ExpandableListView does not appear when Activity starts
Yayy, coding is so much fun. This is my third attempt at creating an ExpandableListView that is linked to a sqllite db. The last version worked ok when retrieving data, but not so great when I needed to delete or edit an item. It was based off of the Api Demo, expandableListView 1 and 2. The Api code suggested that I store my info in an array before it could be put into a ExpandablelistView. I think we can all see an issue with deleting and updating items that are stored in an array. I decided to start over, with something much cleaner.
The following code is my attempt at creating an activity that fills an expandableListView with sqllite db values. However, nothing happens when the activity starts, not even an exception.
I assume that my problem lies within browseView.setAdapter(mAdapter); but I really don't know what Im'm talking about and have no way to prove it! If I change the above to a listadapter it throws a nullpointerexception.
Lastly, it should be noted that my getChildren cursor passes my groupCursor straight though. This is only because I haven't figured out what goes in here! Babysteps.
Am I on the right track here? Thanks for taking a look.
public class BrowseActivity extends ExpandableListActivity {
final private String[] asColumnsToReturn = {
Items.ITEMS_TABLE_NAME + "." + Items.ITEMS_ITEM,
Items.ITEMS_TABLE_NAME + "." + Items.ITEMS_DESC,
Items.ITEMS_TABLE_NAME + "." + Items.ITEMS_ID };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
DbHelper dbh = new DbHelper(this.getApplicationContext());
SQLiteDatabase db = dbh.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Items.ITEMS_TABLE_NAME);
ExpandableListView browseView = (Expand开发者_开发技巧ableListView)findViewById(android.R.id.list);
Cursor mCursor = queryBuilder.query(db, asColumnsToReturn, null, null,
null, null, Items.DEFAULT_SORT_ORDER);
startManagingCursor(mCursor);
SimpleCursorTreeAdapter mAdapter = new SimpleCursorTreeAdapter(this,
mCursor, R.layout.row, R.layout.exprow,
new String[] { Items.ITEMS_ITEM }, new int[] { R.id.txtItem },
R.layout.exprow, R.layout.exprow, new String[] { Items.ITEMS_DESC },
new int[] { R.id.dscItem }) {
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
return groupCursor;
}
};
browseView.setAdapter(mAdapter);
}
}
***START NEW OF NEW CLASS FILE***
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DB_NAME = "itemList.db";
DbHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE " + Items.ITEMS_TABLE_NAME + " ("
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ Items.ITEMS_ITEM + " TEXT," + Items.ITEMS_DESC + " TEXT,"
+ Items.ITEMS_MANU + " TEXT," + Items.ITEMS_UPC + " TEXT," +
Items.ITEMS_NWEIGHT + " TEXT," + Items.ITEMS_AWEIGHT + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onOpen(SQLiteDatabase db){
super.onOpen(db);
}
}
***START OF NEW CLASS FILE***
public class ItemDatabase {
private ItemDatabase() {
}
public static final class Items implements BaseColumns {
private Items() {
}
public static final String ITEMS_TABLE_NAME = "table_itemList";
public static final String ITEMS_ID = "_id";
public static final String ITEMS_ITEM = "item";
public static final String ITEMS_DESC = "description";
public static final String ITEMS_MANU = "manufacturer";
public static final String ITEMS_UPC = "upc";
public static final String ITEMS_NWEIGHT = "netweight";
public static final String ITEMS_AWEIGHT = "actualweight";
public static final String DEFAULT_SORT_ORDER = "item ASC";
}
And the "browse" xml file:
<?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="fill_parent" android:orientation="vertical">
<ExpandableListView
android:id = "@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:clickable="true"
></ExpandableListView>
</LinearLayout>
Its because I passed the Group cursor straight through the getChildrenCursor() method. You have to create a new Cursor inside the method.
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
String tempGroup = groupCursor.getString(groupCursor
.getColumnIndex(Items.ITEMS_ITEM));
DbHelper dbh = new DbHelper(BrowseActivity.this);
SQLiteDatabase db = dbh.getWritableDatabase();
String sqlString = "SELECT " + Items.ITEMS_ID + ", "
+ Items.ITEMS_DESC + ", " + Items.ITEMS_MANU + ", "
+ Items.ITEMS_NWEIGHT + ", " + Items.ITEMS_AWEIGHT + ", "
+ Items.ITEMS_UPC + " FROM " + Items.ITEMS_TABLE_NAME
+ " WHERE " + Items.ITEMS_ITEM + "=" + "'" + tempGroup
+ "'";
Cursor mCursor = db.rawQuery(sqlString, null);
return mCursor;
}
And the Row Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_gravity="center_vertical|right"
android:id="@+id/txtItem"
android:text="Item"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="15dip"
></TextView>
<TextView
android:layout_gravity="center_vertical|right"
android:id="@+id/dscItemTwo"
android:text="Desciption"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textStyle="italic"
android:textColor="#666666"
></TextView>
</LinearLayout>
Finally the expRow Layout(It's a little long, I have alot of table rows. I must admit things are poorly named too. I just wanted to get it working before I made it pretty!):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span = "2"
android:stretchColumns="0"
>
<TableRow
android:layout_height="wrap_content"
android:id="@+id/tableRow1"
android:layout_gravity="right"
android:layout_width="wrap_content"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight = "1"
android:id="@+id/tableRow10"
android:layout_gravity="right"
>
<TextView
android:layout_marginRight="1dip"
android:textColor="#994020"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:text="Manufacturer"
android:id="@+id/manuItem"
></TextView>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:id="@+id/tableRow11"
android:layout_width="wrap_content"
></TableRow>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/tableRow2"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/tableRow9"
android:layout_gravity="right"
android:layout_weight="1"
>
<TextView
android:layout_marginRight="1dip"
android:textColor="#994020"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:text="Description"
android:id="@+id/dscItem"
></TextView>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow8"
></TableRow>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow6"
android:layout_gravity="right"
android:baselineAligned="true">
<TextView
android:layout_marginRight="1dip"
android:textColor="#994020"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Net Weight"
android:id="@+id/nWeightItem"
android:layout_width="wrap_content"
></TextView>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow7"
>
<TextView
android:layout_marginRight="1dip"
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ounces"
android:textStyle="italic"
android:id="@+id/textView1"
></TextView>
</TableRow>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/tableRow12"
android:layout_weight="1"
android:layout_gravity="right"
>
<TextView
android:layout_marginRight="1dip"
android:textColor="#994020"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:text="Actual Weight"
android:id="@+id/aWeightItem"
></TextView>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow13"
>
<TextView
android:layout_marginRight="1dip"
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ounces"
android:textStyle="italic"
android:id="@+id/textView111"
></TextView>
</TableRow>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_gravity="right"
android:id="@+id/tableRow14"
>
<TextView
android:layout_marginRight="1dip"
android:textColor="#994020"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:text="UPC"
android:id="@+id/upcItem"
></TextView>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow15"
></TableRow>
</TableRow>
</TableLayout>
精彩评论