如何在ListView中显示SQLite查询的输出

时间:2013-01-26 10:07:43

标签: android sqlite android-listview

我正在学习Android。我正在尝试学习SQLite数据库,但我遇到了问题。 我无法读取Cursor的输出并在ListView中显示它。这是我的MainActivity.java代码 -

package com.example.addname;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button b1, b2;
    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);
        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());

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

                lv1.setAdapter(adapter);
            }
        });
    }

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

}

这里是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:layout_marginTop="25dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:text="@string/button1" />

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

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="24dp"
        android:text="@string/button2" />

</RelativeLayout>

当我选中“显示”按钮时,我收到错误“不幸的是addname已停止”(addname是我的项目名称)。请帮助。

3 个答案:

答案 0 :(得分:0)

使用SimpleCursorAdapter代替ArrayAdapter。

c = db.rawQuery("SELECT * FROM Names",null);

while(c.isAfterLast() == false){
        c.moveToNext();
}

ListAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.activity_list_item,
                c,
                new String[] {"Name"},
                new int[] {android.R.id.text1},0);

lv1.setAdapter(adapter);

答案 1 :(得分:0)

用此替换do ... while部分

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

答案 2 :(得分:0)

            c = db.rawQuery("SELECT * FROM Names",null);
            int count = c.getCount();
            String values[] = new String[count+1];
            int i = 0;

   while(c.moveToNext())

            { 
                values[i]= c.getString(c.getColumnIndex("Name"));
                i++;
            }




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

            lv1.setAdapter(adapter);