Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display output of SQLite query in ListView
    text
    copied!<p>I am learning Android. I am trying to learn SQLite database but I am getting problem in that. I am unable to read the output of Cursor and display it in ListView. Here is my MainActivity.java code-</p> <pre><code>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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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; } } </code></pre> <p>and here is the activity_main.xml code -</p> <pre><code>&lt;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" &gt; &lt;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" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;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" /&gt; &lt;ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button2" &gt; &lt;/ListView&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>I am getting the error "Unfortunately addname has stopped" (addname is my project's name) when I tab the 'Show' button.Please Help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload