Android – expandablelistview does not appear when the activity is started

Yayy, coding is very interesting. This is my third attempt to create an expandablelistview linked to sqllite database. The last version works normally when retrieving data, but it doesn't work well when I need to delete or edit items. It is based on API demonstration. Expandablelistview 1 and 2. Api code suggests that I store my information in an array, Then I can put it into expandablelistview. I think we can all see the problem of deleting and updating the items stored in the array. I decided to start over and use cleaner things

The following code is my attempt to create an activity that fills the expandablelistview with sqllite DB value. However, there is no response at the beginning of the activity, or even an exception

I assume that my problem is browseview. Setadapter (madapter); But I really don't know what I'm talking about, and I can't prove it! If I change the above content to listadapter, NullPointerException will be thrown

Finally, it should be noted that my getchildren cursor directly passes my groupcursor. This is just because I haven't figured out what's going on here! Babysteps.

Am I on the right track here? Thank you for visiting

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

And "Browse" the 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>

resolvent:

This is because I passed the group cursor directly through the getchildrencursor () method. You must create a new cursor in 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 row layout files:

<?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 a lot of table rows. I must admit that the name of things is also very bad. I just want it to work well before I do it!):

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>