Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

How to load ListView contents from SQLiteDatabase in Android

// 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;
            }
        }

I am using this article from the link:http://www.ezzylearning.com/tutorial.aspxtid=1763429

but i want to load Textview contents from database how to do that..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First create a Weather class, with getter and setter methods

Create a ArrayList weatherlist;

Add the objects to the arraylist.

Pass this arraylist to the adapter


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...