2012-07-19 2 views
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= 

오랫동안 미안하지만 다시이 문제에 관해서는 새로운 것입니다. 감사합니다

+0

MainActivity에서 Null 문자열을받는 것처럼 보입니까? 그렇지? – SALMAN

답변

2

1 액티비티에서 기타로 데이터가 전달되는 것처럼 보입니다.

다음은 완벽하게 작동하는 코드입니다. DATA

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

받는

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

// DATA

를 전송

는 // 그것은 확실히 당신을 도울 것입니다.

감사합니다.