我在android中制作了一个非常简单的Sqlite数据库演示应用程序。一个活动包含一个EditText和一个按钮以及一个textxView,我创建了处理程序类并为mainActivity实现了java代码,但它不工作..我做到了从下面的链接: http ://stdioe.blogspot.in/2012/03/how-to-connect-sqlite-database-in.html
但它在 myActivity.java 文件中的“私有数据库名称;”行中显示错误......请帮助我......它是什么?
我的代码如下:
**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_alignParentTop="true"
android:layout_marginLeft="68dp"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="textPersonName" >
<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:layout_marginTop="43dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_centerVertical="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
DBcontroller.java
package com.example.dbbb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBcontroller extends SQLiteOpenHelper {
public static final String MY_DATABASE ="name";
public static final int VERSION =1;
public DBcontroller(Context context) {
super(context, MY_DATABASE, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE mynames(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS mynames");
onCreate(db);
}
}
main.java
package com.example.dbbb;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
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.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
EditText et;
Button b;
private DB names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
b=(Button)findViewById(R.id.button1);
names = new DB(this);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
AddDATA(name.getText().toString());
Cursor cursor = ShowDATA();
ShowDATA(cursor);
}
finally{
names.close();
}
}
});
}
private void AddDATA(String ResultName){
SQLiteDatabase db = names.getWritableDatabase();
ContentValues datas = new ContentValues();
datas.put("name", ResultName);
db.insertOrThrow("ournames", null, datas);
}
private String[] SELECT = {"id", "name"};
private Cursor ShowDATA(){
SQLiteDatabase db = names.getReadableDatabase();
Cursor cursor = db.query("ournames", SELECT, null, null, null, null, null);
startManagingCursor(cursor);
return cursor;
}
private void ShowDATA(Cursor cursor){
StringBuilder builder = new StringBuilder("RESULTS!:\n");
while(cursor.moveToNext()){
String whatthenameis = cursor.getString((cursor.getColumnIndex("name")));
builder.append(whatthenameis).append("\n");
}
TextView text = (TextView)findViewById(R.id.textView1);
text.setText(builder);
}
}