簡體   English   中英

從SQLite獲取信息並在Listview中顯示

[英]Getting info from SQLite and displaying in Listview

我有從數據庫顯示一些信息的問題。 我應該從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=

對於長篇文章再次感到遺憾,但我是新手。 謝謝

它看起來將1個Activity中的數據傳遞給anther似乎是錯誤的。

這是完美運作的代碼。

//發送數據

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

它一定會幫到你。

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM