我正在学习安卓。我正在尝试学习 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 是我的项目名称)。请帮助。