-1

// 私はこれに本当に慣れていないので、ばかげた質問をしているかもしれません。申し訳ありませんが、助けてください // SQLiteDatabase からリスト TextView のコンテンツをロードしたい // 次の例でそれを行う方法をガイドしてください //This is myアダプター 望ましい結果を得るためにそれを変更する方法

 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

まず、getter メソッドと setter メソッドを使用して Weather クラスを作成します。

ArrayList ウェザーリストを作成します。

オブジェクトを arraylist に追加します。

この配列リストをアダプターに渡します

于 2013-07-16T10:37:25.823 に答える
0

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 ...
.
.}

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 is a widget from the specifique layout for your adapter

于 2013-07-16T10:40:20.203 に答える