我在Android应用程序中创建可扩展列表视图,我想在可扩展列表视图中显示所有Sqlite数据,并在子视图中显示sqlite数据库。我尝试在可扩展列表视图中显示数据,但它无法获取可扩展列表视图中的所有数据。
我在listDataHeader中只有第一个列名,但我也想在每个子视图中获取所有细节。
public class My_Project extends Activity
{
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
DBHelper databaseHelper;
SQLiteDatabase db;
Adapter adp;
Cursor mGroupsCursor;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_project);
databaseHelper = new DBHelper(this);
databaseHelper.onOpen(db);
expListView = (ExpandableListView) findViewById(R.id.ProjectExpandeble_ListView);
fillData();
// get the listview
//expListView = (ExpandableListView) findViewById(R.id.ProjectExpandeble_ListView);
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
//expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
parent.smoothScrollToPosition(groupPosition);
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
private void fillData() {
// set list adapter here
mGroupsCursor = databaseHelper.fetchGroup();
this.startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();
expListView = (ExpandableListView) this.findViewById(R.id.ProjectExpandeble_ListView);
MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(mGroupsCursor, this,
R.layout.list_group, // Your row layout for a group
R.layout.child_item, // Your row layout for a child
new String[] { "project_name" }, // Field(s) to use from group cursor
new int[] { R.id.lblListHeader}, // Widget ids to put group data into
new String[] { "project_id", "project_name", "project_date_created", "project_end_date", "IsActive"}, // Field(s) to use from child cursors
new int[] { R.id.TextView_Projectdetails, R.id.textOne, R.id.textTwo, R.id.textthree, R.id.textFour }); // Widget ids to put child data into
expListView.setAdapter(mAdapter); // set the list adapter.
}
/*private void prepareListData()
{
databaseHelper = new DBHelper(this);
databaseHelper.onOpen(db);
listDataHeader = new ArrayList<String>();
listDataHeader = databaseHelper.getAllProjects();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
List<String> top250 = new ArrayList<String>();
//top250 = databaseHelper.getAllProjects();
//top250.add("The Shawshank Redemption");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.putAll(listDataChild);
}
*/
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
// Your adapter
public MyExpandableListAdapter(Cursor cursor, Context context,int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Cursor childCursor = databaseHelper.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("project_name")));
My_Project.this.startManagingCursor(childCursor);
childCursor.moveToFirst();
return childCursor;
}
}
}
记录Cat信息
06-13 15:23:20.193: W/dalvikvm(17071): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-13 15:23:20.203: E/AndroidRuntime(17071): FATAL EXCEPTION: main
06-13 15:23:20.203: E/AndroidRuntime(17071): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.database.CursorWindow.getLong_native(Native Method)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.database.CursorWindow.getLong(CursorWindow.java:380)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.widget.CursorTreeAdapter$MyCursorHelper.getId(CursorTreeAdapter.java:437)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.widget.CursorTreeAdapter.getGroupId(CursorTreeAdapter.java:192)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:421)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.os.Handler.handleCallback(Handler.java:587)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.os.Looper.loop(Looper.java:123)
06-13 15:23:20.203: E/AndroidRuntime(17071): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-13 15:23:20.203: E/AndroidRuntime(17071): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 15:23:20.203: E/AndroidRuntime(17071): at java.lang.reflect.Method.invoke(Method.java:507)
06-13 15:23:20.203: E/AndroidRuntime(17071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-13 15:23:20.203: E/AndroidRuntime(17071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-13 15:23:20.203: E/AndroidRuntime(17071): at dalvik.system.NativeStart.main(Native Method)