我有android的应用程序,检索sqlite的数据并显示在BaseAdapter数据..什么是最好的方法? [英] i have android app that retrieve data from sqlite and display the data in BaseAdapter .. what is the best way ??
问题描述
我有一个检索SQLite数据库数据并显示在ListView这些数据扩展BaseAdapter Android应用程序。
在这个程序我在绘制文件夹的图像和我的sqlite的包含这些图像的名称的字段。
我的问题是如何获取这些数据,并在ListView ??
显示它我阅读本教程:(的http://www.androidhub4you.com/2012/09/hello-friends-today-i-am-going-to-share.html).
我的问题是有没有其他办法做到这一点?
我将AP preciate任何帮助。
row_list_match_schedulle.xml
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / RelativeLayout1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / textGroup
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:文本=集团
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>
<的TextView
机器人:ID =@ + ID / txtDate
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:文本=日期
机器人:textAppearance =:/>中的Android ATTR / textAppearanceSmall?
<的TextView
机器人:ID =@ + ID / textName1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID / imageView1
机器人:layout_alignParentLeft =真
机器人:文本=名1/>
<的TextView
机器人:ID =@ + ID / textName2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID / imageView2
机器人:layout_alignParentRight =真
机器人:文本=名称2/>
< ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / textLocation
机器人:layout_toRightOf =@ + ID / textName1
机器人:adjustViewBounds =真
机器人:=了maxHeight40dp
机器人:=了maxWidth40dp
机器人:scaleType =fitCenter
机器人:SRC =@可绘制/ algeria_flag/>
< ImageView的
机器人:ID =@ + ID / imageView2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignTop =@ + ID / imageView1
机器人:layout_toLeftOf =@ + ID / textName2
机器人:adjustViewBounds =真
机器人:=了maxHeight40dp
机器人:=了maxWidth40dp
机器人:scaleType =fitCenter
机器人:SRC =@可绘制/ algeria_flag/>
<的TextView
机器人:ID =@ + ID / textLocation
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / textGroup
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =18dp
机器人:文本=位置/>
< / RelativeLayout的>
ItemDetails.java
包com.devleb.expandablelistdemo3;
公共类ItemDetails {
字符串stad_name;
字符串team1;
字符串的Team2;
字符串match_date;
字符串FLAGS1;
字符串flags2;
弦乐群;
公共字符串getStad_name(){
返回stad_name;
}
公共无效setStad_name(字符串stad_name){
this.stad_name = stad_name;
}
公共字符串getTeam1(){
返回team1;
}
公共无效setTeam1(字符串team1){
this.team1 = team1;
}
公共字符串getTeam2(){
返回的Team2;
}
公共无效setTeam2(字符串的Team2){
this.team2 =的Team2;
}
公共字符串getMatch_date(){
返回match_date;
}
公共无效setMatch_date(字符串match_date){
this.match_date = match_date;
}
公共字符串getFlags1(){
返回FLAGS1;
}
公共无效setFlags1(字符串FLAGS1){
this.flags1 = FLAGS1;
}
公共字符串getFlags2(){
返回flags2;
}
公共无效setFlags2(字符串flags2){
this.flags2 = flags2;
}
公共字符串getGroup(){
返回组;
}
公共无效个setgroup(弦乐群){
this.group =组;
}
}
这是我在写一直到现在的 BaseAdapter
CustomAdapterMatchSchedule.java
包com.devleb.expandablelistdemo3;
进口的java.util.ArrayList;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;
公共类CustomAdapterMatchSchedule扩展了BaseAdapter {
LayoutInflater layoutInflater;
ArrayList的< ItemDetails> itemdetailList;
上下文语境;
公共CustomAdapterMatchSchedule(上下文C,ArrayList的< ItemDetails> listOfItem){
this.context = C;
itemdetailList = listOfItem;
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回itemdetailList.size();
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回itemdetailList.get(位置);
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中ARG2){
// TODO自动生成方法存根
ItemDetails itemListDetails = itemdetailList.get(位置);
如果(convertView == NULL){
LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_list_match_schedule,NULL);
}
// Stad_name
TextView的txtStadName =(TextView中)convertView.findViewById(R.id.textLocation);
txtStadName.setText(itemListDetails.getStad_name());
// team1
TextView的txtTeam1 =(TextView中)convertView.findViewById(R.id.textName1);
txtTeam1.setText(itemListDetails.getTeam1());
//的Team2
TextView的txtTeam2 =(TextView中)convertView.findViewById(R.id.textName2);
txtTeam2.setText(itemListDetails.getTeam2());
// FLAG1
TextView的txtflag1 =(TextView中)convertView.findViewById(R.id.imageView1);
txtflag1.setText(itemListDetails.getTeam1());
// FLAG2
返回null;
}
}
这是最合适的方式,特别是因为每个列表视图行包含了很多items.If你想获得一个点击的行中的项目就变成了步行在群雄,C $ c.As不到10行的$就本人而言,它的最佳途径。不要担心,其还我使用。
i have an android application that retrieve data from sqlite database and display these data in a listView that extend a BaseAdapter.
in this app i have images in the drawable folder and i have in the sqlite a field that contain name of these images.
my question is how to retrieve these data and display it in a listView ??
i read this tutorial :(http://www.androidhub4you.com/2012/09/hello-friends-today-i-am-going-to-share.html).
my question is there any other way to do this ??
i will appreciate any help .
row_list_match_schedulle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Group"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentLeft="true"
android:text="name1" />
<TextView
android:id="@+id/textName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignParentRight="true"
android:text="Name2" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textLocation"
android:layout_toRightOf="@+id/textName1"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="@drawable/algeria_flag" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_toLeftOf="@+id/textName2"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="@drawable/algeria_flag" />
<TextView
android:id="@+id/textLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Location" />
</RelativeLayout>
ItemDetails.java
package com.devleb.expandablelistdemo3;
public class ItemDetails {
String stad_name;
String team1;
String team2;
String match_date;
String flags1;
String flags2;
String group;
public String getStad_name() {
return stad_name;
}
public void setStad_name(String stad_name) {
this.stad_name = stad_name;
}
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getMatch_date() {
return match_date;
}
public void setMatch_date(String match_date) {
this.match_date = match_date;
}
public String getFlags1() {
return flags1;
}
public void setFlags1(String flags1) {
this.flags1 = flags1;
}
public String getFlags2() {
return flags2;
}
public void setFlags2(String flags2) {
this.flags2 = flags2;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
}
this is what i wrote until now in the BaseAdapter
CustomAdapterMatchSchedule.java
package com.devleb.expandablelistdemo3;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapterMatchSchedule extends BaseAdapter {
LayoutInflater layoutInflater;
ArrayList<ItemDetails> itemdetailList;
Context context;
public CustomAdapterMatchSchedule(Context c, ArrayList<ItemDetails> listOfItem){
this.context = c;
itemdetailList = listOfItem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemdetailList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemdetailList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ItemDetails itemListDetails = itemdetailList.get(position);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_list_match_schedule, null);
}
//Stad_name
TextView txtStadName = (TextView)convertView.findViewById(R.id.textLocation);
txtStadName.setText(itemListDetails.getStad_name());
//team1
TextView txtTeam1 = (TextView)convertView.findViewById(R.id.textName1);
txtTeam1.setText(itemListDetails.getTeam1());
//team2
TextView txtTeam2 = (TextView)convertView.findViewById(R.id.textName2);
txtTeam2.setText(itemListDetails.getTeam2());
//flag1
TextView txtflag1 = (TextView)convertView.findViewById(R.id.imageView1);
txtflag1.setText(itemListDetails.getTeam1());
//flag2
return null;
}
}
This is the most appropriate way,especially because each listview row contains a lot of items.If you want to get the items on a clicked row it becomes a walk in the pack,less than 10 lines of code.As far as am concerned,its the best way. Don't get worried,its also what i use.
这篇关于我有android的应用程序,检索sqlite的数据并显示在BaseAdapter数据..什么是最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!