0

我在显示数据库中的一些信息时遇到问题。我应该从 EditText 字段中读取一些文本并将它们显示在另一个活动中。我有 ID、姓名、电子邮件、电话和地址作为列名。但我在插入行时出错。对不起,这是一个很长的帖子。这是我的代码:

插入和获取所有信息的代码

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 中显示 Main Activity 中的字符串。当我运行程序时,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=

很抱歉再次发了这么长的帖子,但我对这件事很陌生。谢谢

4

1 回答 1

2

看起来将数据从 1 Activity 传递到花药似乎是错误的。

这是完美运行的代码。

//发送数据

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");

它肯定会帮助你。

谢谢。

于 2012-07-19T09:55:48.623 回答