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
292 views
in Technique[技术] by (71.8m points)

android - Populating a list view from SQLite Database

I'm new to android. What i'm trying to do is to populate a listview from my sqlite database.I know how to do it using arrays. Can somebody help me doing this? I know there is a away to adapt the following code and using it for populating the listview using SQLite but i don't know how to do it.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
        final ListView lt = (ListView)findViewById(R.id.ListView01);
        List<String> w= new ArrayList<String>();
        ArrayAdapter<String> aa;
        w.add("waka");
        w.add("weka");
        w.add("woka");
        aa = new ArrayAdapter<String>(this, R.layout.listW, w);
        lt.setAdapter(aa);
        lt.setTextFilterEnabled(true);
        lt.setOnItemClickListener(new OnItemClickListener() {     
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                  final String aux= (String) lt.getItemAtPosition(position);
                  Intent myIntent = new Intent(infoList.this, tabList.class);
                  startActivity(myIntent);
            }});
    }

Thanks

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can use SimpleCursorAdapter.

Image you have a list view (named 'list_item') item with two fields having ids txtOne and txtTwo respectively. Also, you are having a database table with tow fields - field1 and field2.

When you sgolud use the following adapter call.

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    ctx,
    R.layout.list_item,
    getContentResolver().query(....), // getting cursor for database query here.
    new String[] { "field1", "field2" },
    new int[] { R.id.txtOne, R.id.txtTwo }
);        

This adapter will map a text from field1 to txtOne and from field2 to txtTwo.


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

...