0
我是android開發新手。我發現這個關於可擴展列表視圖的教程,但它只是從一個固定的數據。然後經過一番搜索,我可以用我的數據庫中的數據設置listDataChild。但是現在我被困在如何用數據庫中的數據填充listDataHeader。如何使用數據庫中的數據在可擴展列表視圖中填充listDataHeader?
以下是我迄今所做的:
public class Manage_Students extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
SimpleCursorAdapter cursorAdapter;
Cursor cursor;
private LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.students);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
listDataHeader.add("Subject");
List<String> audio = loginDataBaseAdapter.retrievevalue("g");
listDataChild.put(listDataHeader.get(0),audio); // Header, Child data
}
}
和我的適配器:
public List<String> retrievevalue(String value)
{
ArrayList<String> dataList = new ArrayList<String>();
Cursor cu=db.rawQuery("SELECT Section from Sections where Subjid ='"+value+"'",null);
cu.moveToFirst();
if (!cu.isAfterLast())
{
do
{
dataList.add(cu.getString(0));
}
while (cu.moveToNext());
cu.close();
}
// return the ArrayList that holds the data collected from
// the database.
return dataList;
//
}