如何从SQLite数据库中获取数据并将其显示在ListView中

时间:2013-01-27 22:18:47

标签: android sqlite

我在这里编辑了我之前的帖子。(How to use Android SQLite Database

在这篇文章中,我更改了“显示”按钮的代码。现在我正在尝试访问数据库。我添加了一个ListView并尝试在其中显示数据库的内容。但再次得到同样的错误。 (“不幸的是学习已停止。”)点击“显示”按钮。

这是完整的代码 -

    package com.example.learn;

    import android.os.Bundle;
    import android.app.Activity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;

    public class MainActivity extends Activity {

        Button b1, b2, b3;
        EditText t1;
        ListView lv1;
        SQLiteDatabase db;
        Cursor c;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            b1 = (Button) findViewById(R.id.button1);
            b2 = (Button) findViewById(R.id.button2);
            b3 = (Button) findViewById(R.id.button3);
            t1 = (EditText) findViewById(R.id.editText1);
            lv1 = (ListView) findViewById(R.id.listView1);

            db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
            db.execSQL("CREATE TABLE IF NOT EXISTS Names(Name VARCHAR)");

            b1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    if (t1.getText().toString().equals("")) {
                        t1.setText("Enter Name");
                    }

                    else {
                        String input = t1.getText().toString();
                        db.execSQL("INSERT INTO Names VALUES(" + "'" + input + "'"
                                + ")");
                    }

                }
            });

            b2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    c = db.rawQuery("SELECT * FROM Names", null);
                    int count = c.getCount();

                    String[] values = new String[count + 1];
                    int i = 0;
                    do {
                        values[i] = c.getString(c.getColumnIndex("Name"));
                        i++;
                    } while (c.moveToNext());
                    // Define a new Adapter
                    // First parameter - Context
                    // Second parameter - Layout for the row
                    // Third parameter - ID of the TextView to which the data is
                    // written
                    // Forth - the Array of data

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            getBaseContext(), android.R.layout.simple_list_item_1,
                            android.R.id.text1, values);

                    // Assign adapter to ListView
                    lv1.setAdapter(adapter);

                }
            });

            b3.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    db.execSQL("DELETE FROM Names");
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

    }




and this is activity_main.xml -


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="Add" >

    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="18dp"
        android:text="@string/add" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="@string/show" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:text="@string/del" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1" >

    </ListView>

</RelativeLayout>

我认为

有问题
do {
     values[i] = c.getString(c.getColumnIndex("Name"));
     i++;
   }
while (c.moveToNext());

现在的问题是什么?请帮助。

2 个答案:

答案 0 :(得分:0)

尝试像这样循环:

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
   values[i] = c.getString(c.getColumnIndex("Name"));
}

答案 1 :(得分:0)

我认为你应该试试这段代码。

Cursor cursor = new Cursor();
cursor.moveToFirst();
do{
values[i++] = cursor.getString(cursor.getColumnIndex("name"));
}while(cursor.moveToNext());
cursor.close();