Java 在Android中从SQLite填充文本视图和按钮
我是Android开发的新手,对如何动态显示数据感到困惑 我有一个RelativeLayout活动类型,其中有3个文本视图、3个按钮和一个图像视图 我想要实现的是用SQLite数据库动态地填充这些项 假设我做了一个查询,得到了3行,那么它应该具有我上面提到的所有结构,但使用各自的数据重复了3次 作为自定义列表 在我看到的文档中,许多不是从数据库获取数据,而是从StringArray获取数据,其他人从数据库获取数据,但显示在列表中 从视觉上看,结构是这样的:Java 在Android中从SQLite填充文本视图和按钮,java,android,sqlite,listview,Java,Android,Sqlite,Listview,我是Android开发的新手,对如何动态显示数据感到困惑 我有一个RelativeLayout活动类型,其中有3个文本视图、3个按钮和一个图像视图 我想要实现的是用SQLite数据库动态地填充这些项 假设我做了一个查询,得到了3行,那么它应该具有我上面提到的所有结构,但使用各自的数据重复了3次 作为自定义列表 在我看到的文档中,许多不是从数据库获取数据,而是从StringArray获取数据,其他人从数据库获取数据,但显示在列表中 从视觉上看,结构是这样的: private class Speci
private class SpecialCursorAdapter extends CursorAdapter {
public SpecialCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
//Any other major setup needs to happen here.
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
//Take the view as given in newView, and populate it with the dat afrom cursor.
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LinearLayout row = (LinearLayout) mInflater.inflate(
R.layout.data_row, parent, false);
//Inflate the row
return row;
}
}
现在,形象将永远是金发女人。。。
但我想至少填充文本视图。
从这个意义上说,如果我的查询有两行,那么您应该看到
他们很乐意解释一下我可以用什么方式来完成这项工作?有几种方法可以做到这一点,但您可能需要一个带有光标或适配器的列表视图。CursorAdapter将光标作为输入,并显示单个行的外观。光标适配器的外观如下所示:
private class SpecialCursorAdapter extends CursorAdapter {
public SpecialCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
//Any other major setup needs to happen here.
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
//Take the view as given in newView, and populate it with the dat afrom cursor.
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LinearLayout row = (LinearLayout) mInflater.inflate(
R.layout.data_row, parent, false);
//Inflate the row
return row;
}
}
游标中填充了一个标准查询,有大量的资源可供参考。从膨胀的XML代码中获取ListView指针,然后执行以下操作:
ListView listvew=...;
Cursor cursoer=...;
SpecialCursorAdapter adapter=new SpecialCursorAdapter(this,cursor,0);
listview.setAdpater(adapater);
我经常对这样的大型数据集使用它,而且效果很好。对于这种情况,ListView是最好的选择,您只需要为ListView制作自己的自定义适配器 还要为数据创建数据结构,并将它们放在列表中并将其发送到适配器,然后在适配器中的getView()方法中处理它 创建包含按钮、imageview和TextView的列表项布局,并在自定义适配器中使用它
class MyData{
String someText;
String someText2;
String buttonText;
}
List<MyData> list = new ArrayList<MyData>();
public class MyAdapter extends BaseAdapter{
public MyAdapter(Context c, List<MyData> list){
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate your custom list item layout and put your data
}
}
类MyData{
字符串someText;
字符串someText2;
字符串按钮文本;
}
列表=新的ArrayList();
公共类MyAdapter扩展了BaseAdapter{
公共MyAdapter(上下文c,列表){
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//膨胀自定义列表项布局并放置数据
}
}
一个示例正如我前面所说,我是Android新手,你能给我一些链接来解释你的意思吗?你可以在这里找到一个关于如何用从SQLite数据库检索的数据填充ListView的教程:谢谢,我想你写的东西对我很有帮助!但还有一件事,很难将其调整到可扩展列表中?适配器列表的想法也一样,但您需要为此扩展BaseExpandableListAdapter。您可以在此处找到一个教程,介绍如何使用从SQLite数据库检索的数据填充自定义ListView: