數據庫創建成功了,添加數據也成功了。但在查詢數據跳轉到第二個Activity時是白屏,
什麼也沒查到,調試的結果是沒有進入到while裡面去執行,為什麼沒滿足while條件,
怎麼改?
DatabaseHelper.java
package com.exampl.mysql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DatabaseHelper extends SQLiteOpenHelper{
private Context mContext;
public static final String CREATE_TABLE_SQL="create table dict(_id integer primary key autoincrement,word text,detail text);";
public DatabaseHelper(Context context,String name,CursorFactory factory,int version)
{
super(context,name,factory,version);
mContext=context;
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TABLE_SQL);
Toast.makeText(mContext,"創建成功!",Toast.LENGTH_LONG).show();
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
}
}
MainActivity.java
package com.exampl.mysql;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button insert=null;
private Button search=null;
private EditText word=null;
private EditText detail=null;
private EditText key=null;
DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DatabaseHelper(this,"myDict.db3",null,1);
insert=(Button)findViewById(R.id.insert);
insert.setOnClickListener(this);
search=(Button)findViewById(R.id.search);
search.setOnClickListener(this);
word=(EditText)findViewById(R.id.word);
detail=(EditText)findViewById(R.id.detail);
key=(EditText)findViewById(R.id.key);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert:
String wor=word.getText().toString().trim();
String det=detail.getText().toString().trim();
insertData(dbHelper.getReadableDatabase(),wor,det);
Toast.makeText(this, "添加生詞成功!", Toast.LENGTH_LONG).show();
break;
case R.id.search:
String ke=key.getText().toString().trim();
Cursor cursor=dbHelper.getReadableDatabase().rawQuery("select*from dict where ? or ?", new String[]{"%"+ke+"%","%"+ke+"%"});
Bundle data=new Bundle();
data.putSerializable("date", converCursorToList(cursor));
Intent intent=new Intent(MainActivity.this,Result.class);
intent.putExtras(data);
startActivity(intent);
break;
default:break;
}
}
protected ArrayList<Map<String,String>> converCursorToList(Cursor cursor)
{
ArrayList<Map<String,String>> result=new ArrayList<Map<String,String>>();
while (cursor.moveToNext())
{
Map<String,String> map=new HashMap<String,String>();
map.put("word", cursor.getString(1));
map.put("detail", cursor.getString(2));
result.add(map);
}
return result;
}
private void insertData(SQLiteDatabase db,String word,String detail)
{
db.execSQL("insert into dict(word,detail) values(?,?)",new String[]{word,detail});
}
@Override
public void onDestroy()
{
super.onDestroy();
if (dbHelper!=null)
dbHelper.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Result.java
package com.exampl.mysql;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Result extends Activity {
private TextView jieguo=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.line);
jieguo=(TextView)findViewById(R.id.jieguo);
Bundle bundle=getIntent().getExtras();
jieguo.setText(bundle.getString("date"));
}
}
首先,你的sql語句不對,你可以依照樓上這位大神的方法修改你的sql語句,這樣的話大概就能解決你所說的while()循環不執行的問題;
再一個,我照你的代碼大概的跑了一遍,第二個界面之所以不顯示任何信息是因為你的bundle用的不對。我不知道你為什麼要傳List,還要嵌套Map集合
但是就算是非這樣不可,跳轉後的界面我覺得因該這樣實現
不知道能不能解決你的問題。