如何从Android的SQLiteDatabase加载ListView内容

问题描述:

//我真的很新,所以可能会问一些愚蠢的问题,所以很抱歉,但plz帮助 //我想从SQLiteDatabase加载列表TextView的内容 //请指导它如何与下面的示例 做//这是我的适配器如何修改它想要的结果如何从Android的SQLiteDatabase加载ListView内容

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从数据库的内容如何做到这一点..

首先创建一个Weather类,使用getter和setter方法

创建一个ArrayList weatherlist;

将对象添加到数组列表。

传递此ArrayList到适配器

+0

公共类天气{ public int icon; public String title; public Weather(){ super(); } public Weather(int icon,String title){ super(); this.icon =图标; this.title = title; } } – sudhier

+0

添加getter和setter方法。 在Adapter getView方法中 Weather obj = this.getItem(position); holder.txtTitle.setText(obj.getTitle()); holder.imgIcon.setImageResource(obj.getIcon()); – zacharia

首先您将创建类databaseconnect扩展sqlitehelper ......和MACKE一个函数的函数从数据库到底得的第二, :类WeatherAdapter getView,你将执行在数据库类中定义的函数....自动适配器,当你午餐的应用程序,自动从数据库中获取所有信息,并在列表视图...它希望它可以帮助你...祝你好运......。

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,你作出这样的:

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()); 

. 
.. 
.} 

NB:R.id.firstname从specifique布局适配器一个widget