Activity启动时不会出现ExpandableListView

时间:2011-06-29 19:54:33

标签: android expandablelistview

Yayy,编码非常有趣。这是我创建一个链接到sqllite数据库的ExpandableListView的第三次尝试。最后一个版本在检索数据时工作正常,但在我需要删除或编辑项目时效果不是很好。它基于Api演示,expandableListView 1和2.Api代码建议我将我的信息存储在一个数组中,然后才能将它放入ExpandablelistView。我想我们都可以看到删除和更新存储在数组中的项目的问题。我决定重新开始,用更清洁的东西。

以下代码是我尝试创建一个用sqllite db值填充expandableListView的活动。但是,活动开始时没有任何反应,甚至不是例外。

我认为我的问题在于browseView.setAdapter(mAdapter);但我真的不知道我在说什么,也无法证明这一点!如果我将上面的内容更改为listadapter,则会抛出nullpointerexception。

最后,应该注意我的getChildren游标直接通过了我的groupCursor。这只是因为我还没弄清楚这里发生了什么! Babysteps。

我在这里走在正确的轨道上吗?谢谢参观。

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

和“浏览”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="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>

1 个答案:

答案 0 :(得分:3)

因为我直接通过了getChildrenCursor()方法传递了Group游标。您必须在方法中创建一个新的Cursor。

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

    }

行布局文件:

<?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>

最后是expRow布局(它有点长,我有很多表行。我必须承认事情的名字也很糟糕。我只是想在它变得漂亮之前让它工作!) :

<?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>