Android 如何在ExpandableListView中加载sqlite数据库
我想创建一个应用程序,在ExpandableListView中加载数据,如下所示: 我找到了这个 您能告诉我如何从sqllite数据库动态加载Expandablelistview,以及如何在Android 如何在ExpandableListView中加载sqlite数据库,android,sqlite,expandablelistview,Android,Sqlite,Expandablelistview,我想创建一个应用程序,在ExpandableListView中加载数据,如下所示: 我找到了这个 您能告诉我如何从sqllite数据库动态加载Expandablelistview,以及如何在Expandablelistview中显示多个表及其子项吗 public class MainActivity extends Activity { ExpandableListAdapter listAdapter; ExpandableListView expListView; List<Str
Expandablelistview
中显示多个表及其子项吗
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
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();
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;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
公共类MainActivity扩展活动{
可扩展列表适配器;
ExpandableListView解释视图;
列表列表数据头;
HashMapListDataChild;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取列表视图
expListView=(ExpandableListView)findViewById(R.id.lvExp);
//准备列表数据
prepareListData();
listAdapter=新的ExpandableListAdapter(此,listDataHeader,listDataChild);
//设置列表适配器
expListView.setAdapter(listAdapter);
//Listview组单击侦听器
expListView.setOnGroupClickListener(新OnGroupClickListener(){
@凌驾
公共布尔值onGroupClick(ExpandableListView父视图,视图v,
int groupPosition,长id){
//Toast.makeText(getApplicationContext(),
//“组单击”+listDataHeader.get(groupPosition),
//吐司。长度(短)。show();
返回false;
}
});
//Listview组扩展侦听器
expListView.setOnGroupExpandListener(新的OnGroupExpandListener(){
@凌驾
public void onGroupExpand(int groupPosition){
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition)+“Expanded”,
吐司。长度(短)。show();
}
});
//Listview组压缩侦听器
expListView.setOnGroupCollapseListener(新OnGroupCollapseListener(){
@凌驾
公共void-onGroupCollapse(int-groupPosition){
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition)+“折叠”,
吐司。长度(短)。show();
}
});
//子单击侦听器上的Listview
setOnChildClickListener(新的OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
//TODO自动生成的方法存根
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+listDataChild.get(
listDataHeader.get(groupPosition)).get(
儿童位置),吐司。长度(短)
.show();
返回false;
}
});
}
/*
*准备列表数据
*/
私有void prepareListData(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
//添加子数据
添加(“前250名”);
添加(“现在显示”);
添加(“即将推出…”);
//添加子数据
List top250=新的ArrayList();
top250.添加(“肖申克的救赎”);
添加(“教父”);
top250.添加(“教父:第二部分”);
top250.添加(“低俗小说”);
添加(“好、坏、丑”);
添加(“黑暗骑士”);
添加(“12个愤怒的男人”);
List nowShowing=新建ArrayList();
现在显示。添加(“变戏法”);
添加(“卑鄙的我2”);
现在显示。添加(“Turbo”);
现在显示。添加(“成年人2”);
现在显示。添加(“红色2”);
现在显示。添加(“狼獾”);
List comingSoon=新建ArrayList();
很快就来。添加(“2支枪”);
很快就来。添加(“蓝精灵2”);
即将到来。添加(“现在的壮观”);
即将到来。添加(“峡谷”);
即将出版。添加(“欧罗巴报告”);
listDataChild.put(listDataHeader.get(0),top250);//头,子数据
listDataChild.put(listDataHeader.get(1),现在显示);
listDataChild.put(listDataHeader.get(2),即将出现);
}
}