我的数据库中有一个表。这是一个公司目录Android应用程序。我想按类别对公司进行分组。类别是我表格的属性之一。我正在关注这个example。我面临的问题是实现fetchGroup()方法。 这是因为在该示例中,它们具有用于组的单独表。如果我想按其中一个属性进行分组,如何实现此方法?
这是我的活动课程:
public class CompaniesList extends ListActivity {
protected SQLiteDatabase db;
protected Cursor cursor;
protected ExpandableListAdapter mAdapter;
protected ExpandableListView expListView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
db = (new DatabaseHelper(this)).getWritableDatabase();
prepareListData();
expListView.setOnGroupClickListener(new OnGroupClickListener() {
// some code, doesn't matter
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
// some code, doesn't matter
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
// some code, doesn't matter
});
expListView.setOnChildClickListener(new OnChildClickListener() {
// some code, doesn't matter
});
}
private void prepareListData() {
mGroupsCursor = db.fetchGroup();
getActivity().startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();
ExpandableListView elv = (ExpandableListView)getActivity().findViewById(android.R.id.lvExp);
mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
R.layout.company_group_item, R.layout.company_list_item,
new String[] { "????" }, new int[] { android.R.id.????? },
new String[] { "name", "address" }, new int[] { R.id.name, R.id.address});
lv.setAdapter(mAdapter);
}
}
class MyAdapter extends SimpleCursorTreeAdapter {
public MyAdapter(Context context, Cursor cursor, int groupLayout,
String[] groupFrom, int[] groupTo, int childLayout,
String[] childFrom, int[] childTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childFrom, childTo);
}
protected Cursor getChildrenCursor(Cursor groupCursor) {
Cursor childCursor = db.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("id_category"));
getActivity().startManagingCursor(childCursor);
childCursor.moveToFirst();
return childCursor;
}
}
}
这是我的db helper类:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "employee_directory2";
protected SQLiteDatabase db;
protected Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String s;
try {
Toast.makeText(context, "1", 2000).show();
InputStream in = context.getResources().openRawResource(R.raw.sql);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList statements = doc.getElementsByTagName("statement");
for (int i=0; i<statements.getLength(); i++) {
s = statements.item(i).getChildNodes().item(0).getNodeValue();
db.execSQL(s);
}
} catch (Throwable t) {
Toast.makeText(context, t.toString(), 50000).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS employees");
onCreate(db);
}
// THIS IS WRONG. I NEED TO FETCH THE NAME OF CATEGORIES. CATEGORIES - ONE OF THE // ATTRIBUTES IN MY DB TABLE.
public Cursor fetchGroup() {
String query = "SELECT * FROM employee";
return db.rawQuery(query, null);
}
public Cursor fetchChildren(String department) {
String query = "SELECT * FROM employee WHERE id_department = '" + department + "'";
return db.rawQuery(query, null);
}
}