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 };@OverrIDepublic 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);} @OverrIDepublic 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)");} @OverrIDepublic voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {}@OverrIDepublic 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"?><linearLayoutxmlns: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>
解决方法:
这是因为我直接通过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>
总结 以上是内存溢出为你收集整理的android – 当Activity启动时不会出现ExpandableListView全部内容,希望文章能够帮你解决android – 当Activity启动时不会出现ExpandableListView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)