我已经在 android 中创建了一个带有单个活动的项目,我想通过按图片中显示的“插入按钮”将新记录添加到数据库中,但是我希望将新记录保存到数据库后,ListView 更新并显示添加到列表最后位置的记录...
当我运行此代码时发生意外错误并关闭错误或任何建议...
我把所有的代码和布局放在这里...
MainActivity.java
package com.example.db;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button Btninser;
SQLiteDatabase db;
EditText etName, etPhone;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etPhone = (EditText) findViewById(R.id.etPhone);
Btninsert = (Button) findViewById(R.id.btnInsert);
lv = (ListView) findViewById(R.id.listView1);
Btninsert.setOnClickListener(this);
Open_Create();
}
public void Open_Create() {
db = openOrCreateDatabase("MyDB1", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS MyTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, LastName VARCHAR,Phone VARCHAR);");
}
public void Insert_Info() {
db.execSQL("INSERT INTO MyTable (LastName,Phone) VALUES('" + etName.getText().toString()
+ "','" + etPhone.getText().toString() + "');");
Populate_ListView();
// db.close();
Toast.makeText(this, "Data has been saved!", Toast.LENGTH_LONG).show();
}
public void Populate_ListView() {
Cursor cursor = db.rawQuery("SELECT LastName,Phone FROM MyTable", null);
startManagingCursor(cursor);
String[] from = new String[] { "LastName", "Phone" };
int[] to = new int[] { R.id.tvItemListName, R.id.tvItemListPhone };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item_list, cursor, from, to);
lv.setAdapter(adapter);
}
这是XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Name" />
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Phone" />
<EditText
android:id="@+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
</EditText>
<Button
android:id="@+id/btnInsert"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Insert" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="21dp" >
</ListView>
</RelativeLayout>
我已经添加了来自 DDMS 的错误,正如您在错误页面中看到的那样它提到了一个我从未拥有过的字段“ _id ” !!!!:( 太让我困惑了...