如何从SQLiteDatabase加载内容的ListView在Android中 [英] How to load ListView contents from SQLiteDatabase in Android
问题描述
//我真的很新的这个,所以可能会问一些愚蠢的问题很抱歉,但plz帮助
//我要加载从SQLiteDatabase名单的TextView的内容
//请指导它如何与下面的例子来完成
//这是我的适配器如何修改它想要的结果。
// I am really new to this,so might be asking some silly questions so sorry but plz help //I want to load content of lists TextView from SQLiteDatabase //please guide how it could be done with following Example //This is my adapter how to modify it for desired result
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
public class MainActivity extends Activity {
private ListView listView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//I want to load these contents in lists textview but from sqlite db,how to do it
//Help appreciated Thanks...
Weather weather_data[] = new Weather[]
{
new Weather(R.drawable.weather_cloudy, "Cloudy"),
new Weather(R.drawable.weather_showers, "Showers"),
new Weather(R.drawable.weather_snow, "Snow"),
new Weather(R.drawable.weather_storm, "Storm"),
new Weather(R.drawable.weather_sunny, "Sunny")
};
WeatherAdapter adapter = new WeatherAdapter(this,
R.layout.listview_item_row, weather_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
This is my weather class
public class Weather {
public int icon;
public String title;
public Weather(){
super();
}
public Weather(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
我用这从文章的链接: http://www.ezzylearning.com/tutorial .aspxtid = 1763429
但我想从数据库中加载的TextView内容,如何做到这一点..
but i want to load Textview contents from database how to do that..
推荐答案
首先,您将创建类databaseconnect扩展sqlitehelper ......和MACKE一个函数从数据库最终获得第二,
在功能:类WeatherAdapter getView,您将执行数据库类中定义的函数.... automaicly适配器时,你的午餐appllication,全自动从数据库中的所有信息获取和在ListView MACKE它...希望它帮助你......好运气......
firstly you will create class databaseconnect extends sqlitehelper ... and macke a functions to get from database end second , in the function : getView in class WeatherAdapter , you will execute the functions defined in database class .... automaicly the adapter when you lunch the appllication , automaticly it get from database all informations and macke it in the listview ... hope it helped you ... good luck ... .
public class DataBaseConect extends SQLiteOpenHelper{
private static final String DATABASE_NAME="constants.db";
private static final int SCHEMA=1;
public DataBaseConect(Context context)
{
super(context, DATABASE_NAME, null, SCHEMA);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE contacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,nom TEXT,prenom TEXT,mail TEXT,phone INTEGER,status TEXT,_id_V INTEGER)");
}
.
.
.
.here you put your functions to get from database ...
.
.}
和在类addapter,你让这样的:
and in the class addapter , you make like that :
public class ContactsAdapter extends ArrayAdapter<contact> {
public ListView rideHistoryListView;
public static Context context = null;
public ContactsAdapter(Context context, ArrayList<contact> list) {
super(context, android.R.layout.simple_list_item_1, list);
this.context = context;
}
public View getView(int position, View convertView, final ViewGroup parent) {
View row = convertView;
final contact c;
if (row == null) {
LayoutInflater inf = ((Activity) super.getContext())
.getLayoutInflater();
row = inf.inflate(R.layout.myadapter, parent, false);
}
c = getItem(position);
((TextView) row.findViewById(R.id.firstname)).setText(c.getNom());
.
..
.}
N.B:R.id.firstname距离specifique布局窗口小部件适配器
N.B: R.id.firstname is a widget from the specifique layout for your adapter
这篇关于如何从SQLiteDatabase加载内容的ListView在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!