0

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

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.

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.

3
  • You might also want to post logcat. Commented Jan 27, 2013 at 22:40
  • How do I post it? I mean when I copy and paste it here in comment box, it is difficult to read. Commented Jan 28, 2013 at 5:35
  • mo, add it to your question. Commented Jan 28, 2013 at 5:40

2 Answers 2

0

Try looping like this:

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
   values[i] = c.getString(c.getColumnIndex("Name"));
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should try this code.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.