0
我在顯示數據庫中的某些信息時遇到了問題。我應該閱讀EditText字段中的一些文本並將它們顯示在另一個活動中。我有id,姓名,電子郵件,電話和地址作爲列名。但是我在插入行時遇到了錯誤。很抱歉它是一個漫長的post.Here我的代碼:從SQLite獲取信息並在列表視圖中顯示
插入和獲取所有信息
public class InformationDataSource {
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_EMAIL, MySQLiteHelper.COLUMN_PHONE, MySQLiteHelper.COLUMN_ADDRESS};
public InformationDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Info createInfo(String name,String email,String phone,String address) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_EMAIL, email);
values.put(MySQLiteHelper.COLUMN_PHONE, phone);
values.put(MySQLiteHelper.COLUMN_ADDRESS, address);
long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Info newInfo = cursorToInfo(cursor);
cursor.close();
return newInfo;
}
public List<Info> getAllInfos() {
List<Info> Infos = new ArrayList<Info>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Info Info = cursorToInfo(cursor);
Infos.add(Info);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return Infos;
}
private Info cursorToInfo(Cursor cursor) {
Info Info = new Info();
Info.setId(cursor.getLong(0));
Info.setName(cursor.getString(1));
Info.setEmail(cursor.getString(2));
Info.setPhone(cursor.getString(3));
Info.setAddress(cursor.getString(4));
return Info;
}
}
MainActivity類別的代碼。我在這裏從EditText字段獲取字符串。
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.add);
EditText nametext = (EditText) this.findViewById(R.id.NameText);
final String nt = nametext.getText().toString();
EditText emailtext = (EditText) this.findViewById(R.id.MailText);
final String et = emailtext.getText().toString();
EditText phonetext = (EditText) this.findViewById(R.id.PhoneText);
final String pt = phonetext.getText().toString();
EditText addresstext = (EditText) this.findViewById(R.id.AddressText);
final String at = addresstext.getText().toString();
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString("NT",nt);
Bundle bundle2 = new Bundle();
bundle2.putString("MT",et);
Bundle bundle3 = new Bundle();
bundle3.putString("PT",pt);
Bundle bundle4 = new Bundle();
bundle4.putString("AT",at);
Intent myIntent = new Intent(view.getContext(), Activity2.class);
myIntent.putExtras(bundle);
myIntent.putExtras(bundle2);
myIntent.putExtras(bundle3);
myIntent.putExtras(bundle4);
startActivityForResult(myIntent, 0);
}
});
}
}
這是我的其他活動。使用MainActivity中的字符串。
public class Activity2 extends ListActivity {
private InformationDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle bundle = this.getIntent().getExtras();
String nt = bundle.getString("NT");
Bundle bundle2 = this.getIntent().getExtras();
String et = bundle2.getString("ET");
Bundle bundle3 = this.getIntent().getExtras();
String pt = bundle3.getString("PT");
Bundle bundle4 = this.getIntent().getExtras();
String at = bundle4.getString("AT");
datasource = new InformationDataSource(this);
datasource.open();
@SuppressWarnings("unchecked")
ArrayAdapter<Info> adapter2 = (ArrayAdapter<Info>) getListAdapter();
Info info = null;
// Save the new info to the database
info = datasource.createInfo(nt,et,pt,at);
adapter2.add(info);
adapter2.notifyDataSetChanged();
List<Info> values = datasource.getAllInfos();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Info> adapter = new ArrayAdapter<Info>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
還有另一個叫Info的類,我用它來保存信息。我認爲我的錯誤是在Activity2.class中。我無法在ListView中的主要活動中顯示字符串。 當我運行程序時,MainActivity可以正常工作,但在輸入信息並按下「添加」按鈕後,出現錯誤。這是第一個錯誤。
07-19 09:44:19.013: W/KeyCharacterMap(545): No keyboard for id 0
07-19 09:44:19.013: W/KeyCharacterMap(545): Using default keymap:
/system/usr/keychars/qwerty.kcm.bin
07-19 09:44:27.974: E/Database(545): Error inserting Name= Phone= Email=null Address=
所以很抱歉再次發帖,但我對這件事很陌生。謝謝
它看起來是從MainActivity接收空字符串?是這樣嗎? – SALMAN 2012-07-19 09:52:36