如何获得数据格式的SQLite数据库,并在ListView中显示它 [英] How to get data form SQLite database and display it in ListView

查看:160
本文介绍了如何获得数据格式的SQLite数据库,并在ListView中显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里编辑我的previous职位。(如何使用Android的SQLite数据库

I have edited my previous post here.(How to use Android SQLite Database)

在这篇文章中我已经改变了$ C $下显示按钮。现在,我想访问数据库。我增加了一个ListView,并试图在其中显示数据库的内容。但同样得到同样的错误。在攻显示按钮(不幸的是学会已经停止。)。

In this post I have changed the code for 'Show' button. Now I am trying to access the database. I have added a ListView and trying to display the content of database in it. But again getting the same error. ("Unfortunately learn has stopped.") on tapping the Show button.

下面是完整的code -

Here is the complete code -

    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>

我觉得有什么不妥。

I think something wrong with

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

这是什么问题呢?请帮助。

what is the problem now? Please Help.

推荐答案

试试循环是这样的:

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

这篇关于如何获得数据格式的SQLite数据库,并在ListView中显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆