-1

//我对此真的很陌生,所以可能会问一些愚蠢的问题,很抱歉,但请帮助 //我想从 SQLiteDatabase 加载列表 TextView 的内容 //请指导如何使用以下示例完成 //这是我的适配器如何修改它以获得所需的结果

 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 内容如何做到这一点..

4

2 回答 2

0

首先创建一个 Weather 类,带有 getter 和 setter 方法

创建一个 ArrayList 天气列表;

将对象添加到数组列表。

将此数组列表传递给适配器

于 2013-07-16T10:37:25.823 回答
0

首先,您将创建类 databaseconnect 扩展 sqlitehelper ...并创建一个函数以从数据库端获取第二个,在函数中:在 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()); 

.
..
.}

注意:R.id.firstname 是适配器特定布局中的一个小部件

于 2013-07-16T10:40:20.203 回答