从SQLite获取信息并在列表视图中显示
问题描述:
我在显示数据库中的某些信息时遇到了问题。我应该阅读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=
所以很抱歉再次发帖,但我对这件事很陌生。谢谢
答
它看起来从1活动传递数据到花药似乎是错误的。
这是完美的代码。
//发送数据
String fName_temp = yourObject.getFname();
String lName_temp = yourObject.getLname();
String age_temp = yourObject.getAge();
String address_temp = yourObject.getAddress();
Intent i = new Intent(this, ToClass.class);
i.putExtra("fname", fName_temp);
i.putExtra("lname", lName_temp);
i.putExtra("age", age_temp);
i.putExtra("address", address_temp);
startActivity(i);
//接收数据的
Intent intent = getIntent();
String fname = intent.getExtras().getString("fname");
String lname = intent.getExtras().getString("lname");
String age = intent.getExtras().getString("age");
String address = intent.getExtras().getString("address");
它一定会帮助你的。
谢谢。
它看起来是从MainActivity接收空字符串?是这样吗? – SALMAN 2012-07-19 09:52:36