我想将db中的数据导入listview。
但它有错误我无法纠正这个问题。
请帮帮我
感谢!!
这是我的代码
package com.BITS.DB;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ChoicePray extends ListActivity {
AlertDialog dialog;
EditText wordNameSearch;
TextView typeShow;
ImageView imageSearch;
static Database db;
static String database;
String wordSearch = null;
private int idWord;
public String type="bits";
public static Context context;
private static final ArrayList<String> ListviewContent = new ArrayList<String>();
private static final ArrayList<Integer> ListID = new ArrayList<Integer>();
ListViewAdapter listAdap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choicepray);
typeShow = (TextView) findViewById(R.id.groupshow);
wordNameSearch = (EditText) findViewById(R.id.namePraySearch);
imageSearch = (ImageView) findViewById(R.id.imageSearch);
context = getBaseContext();
db = new Database(this);
showAllword();
/*typeShow.setText("Bits");
listAdap = new ListViewAdapter(this);
listAdap.notifyDataSetChanged();
setListAdapter(listAdap);
SearchOnButton();*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:{
Intent a = new Intent(getBaseContext(),Insert_Form.class);
startActivity(a);
break;
}
case R.id.bits:{
type="bits";
typeShow.setText("bits");
showAllword();
break;
}
case R.id.pray1:{
type="pray1";
typeShow.setText("pray1");
showAllword();
break;
}
case R.id.pray2:{
type="2";
typeShow.setText("pray2");
showAllword();
break;
}
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setMessage("ARE YOU SURE EXIT ?");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
{ finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
dialog.cancel();
}
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void SearchOnButton(){
imageSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
wordSearch = wordNameSearch.getText().toString();
ListID.clear();
ListviewContent.clear();
db = new Database(context);
db.open();
Cursor c = db.searchWord(wordSearch);
if (c.moveToFirst()) {
do {
displayPray(c);
} while (c.moveToNext());
}
db.close();
wordNameSearch.setText("");
typeShow.setText("All");
listAdap.notifyDataSetChanged();
setListAdapter(listAdap);
}
});
}
public void ButtonAlertDialog(final int position){
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setMessage("ARE YOU SURE DELETE WORD ?");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
{
Log.v("Delete", "delete id" + ListID.get(position));
db.open();
boolean flag = db.deleteWord(ListID.get(position));
if (flag) {
Toast.makeText(context, "Delete Succeed.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Delete Failed",
Toast.LENGTH_SHORT).show();
}
db.close();
showAllword();
}
dialog.cancel();
}
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
public void showAllword() {
ListID.clear();
ListviewContent.clear();
db = new Database(context);
db.open();
Cursor c = db.getAllWord(type);
if (c.moveToFirst()) {
do {
displayPray(c);
} while (c.moveToNext());
}
db.close();
listAdap = new ListViewAdapter(this);
listAdap.notifyDataSetChanged();
setListAdapter(listAdap);
}
public static void displayPray(Cursor c) {
ListviewContent.add(c.getString(1));
ListID.add(Integer.parseInt(c.getString(0)));
}
private class ListViewAdapter extends BaseAdapter {
public LayoutInflater mInflater;
public ListViewAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return ListviewContent.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ListContent holder;
View v = convertView;
if (v == null || (v.getTag()==null)) {
v = mInflater.inflate(R.layout.list_item, null);
holder = new ListContent();
holder.text = (TextView) v.findViewById(R.id.TextView01);
v.setTag(holder);
}
else {
holder = (ListContent) v.getTag();
}
v.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent ex = new Intent(getBaseContext(), Explan.class);
idWord = ListID.get(position);
ex.putExtra("idWord", idWord);
ex.putExtra("type", type);
startActivity(ex);
}
});
holder.text.setText(ListviewContent.get(position));
v.setOnLongClickListener(new OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
ButtonAlertDialog(position);
return true;
}
});
return v;
}
private class ListContent {
TextView text;
}
}
}
package com.BITS.DB;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Database {
public String Serach = null;
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_WORD = "word";
public static final String KEY_TYPE = "type";
public static final String KEY_EXPLANTION = "Explanation";
public static final String TAG = "DatabasePray";
public static final String DATABASE_NAME = "BITSonMobiles";
public static final String DATABASE_TABLE = "bits_table";
public static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table pray"
+ "(_id integer primary key autoincrement," + "word char(40)NOT NULL,"
+ "Explanation text NOT NULL);";
private Context context = null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public Database(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
public Database open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertWord(String name, String word, String type, String Explanation) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_WORD, word);
initialValues.put(KEY_TYPE, type);
initialValues.put(KEY_EXPLANTION, Explanation);
return db.replace(DATABASE_TABLE, null, initialValues);
}
public boolean deleteWord(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public boolean updateWord(long rowId, String name, String type, String word, String Explanation) {
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_WORD, word);
args.put(KEY_TYPE, type);
args.put(KEY_EXPLANTION, Explanation);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllWord(String type) {
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_NAME,
KEY_WORD,
KEY_TYPE,
KEY_EXPLANTION },
KEY_TYPE + "=" + type ,
null,
null,
null,
KEY_WORD);
}
public Cursor searchWord(String Wordcom) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_NAME,
KEY_WORD,
KEY_TYPE,
KEY_EXPLANTION },
KEY_WORD + "=" + Wordcom,
null,
null,
null,
KEY_WORD,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getWord(long rowId) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_NAME,
KEY_WORD,
KEY_TYPE,
KEY_EXPLANTION },
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version" + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
}
ERROR/AndroidRuntime(274): FATAL EXCEPTION: main
ERROR/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.BITS.DB/com.BITS.DB.ChoicePray}: android.database.sqlite.SQLiteException: no such table: bits_table: , while compiling: SELECT _id, name, word, type, Explanation FROM bits_table WHERE type=bits ORDER BY word
ERROR/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(274): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
ERROR/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(274): Caused by: android.database.sqlite.SQLiteException: no such table: bits_table: , while compiling: SELECT _id, name, word, type, Explanation FROM bits_table WHERE type=bits ORDER BY word
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
ERROR/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
ERROR/AndroidRuntime(274): at com.BITS.DB.Database.getAllWord(Database.java:72)
ERROR/AndroidRuntime(274): at com.BITS.DB.ChoicePray.showAllword(ChoicePray.java:188)
ERROR/AndroidRuntime(274): at com.BITS.DB.ChoicePray.onCreate(ChoicePray.java:53)
ERROR/AndroidRuntime(274): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
ERROR/AndroidRuntime(274): ... 11 more
那么,任何人都可以帮忙解决这个问题吗?提前谢谢!
答案 0 :(得分:2)
您创建的表格pray
且DATABASE_TABLE = "bits_table"
不存在