2014-09-30 7 views
1

사람들의 정보를 수집하고 데이터베이스에 추가 할 수있는 앱을 만들고 싶습니다. 하지만 선택한 라디오 버튼 값을 데이터베이스에 삽입하는 데 문제가 있습니다. 데이터베이스에 라디오 버튼 값을 삽입하십시오.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.mythirdapp.MainActivity" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="14dp" 
     android:text="@string/last_name" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/first_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@+id/textView1" 
     android:ems="10" 
     android:inputType="textPersonName" > 

     <requestFocus android:layout_width="match_parent" /> 

    </EditText> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="14dp" 
     android:text="@string/first_name" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="16dp" 
     android:onClick="add" 
     android:text="@string/add" /> 

    <Button 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_alignRight="@+id/last_name" 
     android:onClick="show" 
     android:text="@string/show" /> 

    <EditText 
     android:id="@+id/last_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/first_name" 
     android:layout_below="@+id/textView1" 
     android:ems="10" 
     android:inputType="textPersonName" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_below="@+id/last_name" 
     android:layout_marginTop="14dp" 
     android:text="@string/gender" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <RadioGroup 
     android:id="@+id/radio_gender" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignRight="@+id/Button01" 
     android:layout_alignTop="@+id/textView3" 
     android:layout_toRightOf="@+id/button1" > 

     <RadioButton 
      android:id="@+id/radio_female" 
      android:text="@string/radio_female" 
      android:onClick="RadioButtonClicked" /> 

     <RadioButton 
      android:id="@+id/radio_male" 
      android:text="@string/radio_male" 
      android:onClick="RadioButtonClicked"/> 

    </RadioGroup> 

</RelativeLayout> 

public class MainActivity extends Activity { 
    SQLiteDatabase pf; 

    TableRow tablerow; 
    TextView t,t1,t2,t3,t4,t5;  
    String first_name,last_name; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    pf=openOrCreateDatabase("Profile",MODE_PRIVATE,null); 
    pf.execSQL("CREATE TABLE IF NOT EXISTS Persone(first_name VARCHAR,last_name VARCHAR,Usergender VARCHAR);"); 
} 

public void RadioButtonClicked(View view) { 

    //This variable will store whether the user was male or female 
    String userGender=""; 
    // Check that the button is now checked? 
    boolean checked = ((RadioButton) view).isChecked(); 

    // Check which radio button was clicked 
    switch(view.getId()) { 
     case R.id.radio_female: 
      if (checked) 
       userGender = "female"; 
      break; 
     case R.id.radio_male: 
      if (checked) 
       userGender = "male"; 
      break; 
    } 
    pf.execSQL("INSER INTO Persone VALUES('"+userGender+"');"); 
} 

public void add(View view){ 
    EditText fn = (EditText)findViewById(R.id.first_name); 
    EditText ln = (EditText)findViewById(R.id.last_name); 

    first_name=fn.getText().toString(); 
    last_name =ln.getText().toString(); 

    pf.execSQL("INSERT INTO Persone VALUES('"+first_name+"','"+last_name+"');"); 
} 



public void show(View view){ 
    Cursor c = pf.rawQuery("SELECT * FROM Persone", null); 
    int count = c.getCount(); 
    c.moveToFirst(); 
    TableLayout tablelayout = new TableLayout(getApplicationContext()); 
    tablelayout.setVerticalScrollBarEnabled(true); 
    TableRow tablerow; 
    TextView t,t1,t2,t3,t4,t5; 
    tablerow = new TableRow(getApplicationContext()); 

    t = new TextView(getApplicationContext()); 
    t.setText("First Name"); 
    t.setTextColor(Color.RED); 
    t.setTypeface(null,Typeface.BOLD); 
    t.setPadding(20,20,20,20); 
    tablerow.addView(t); 

    t3 = new TextView(getApplicationContext()); 
    t3.setText("Last Name"); 
    t3.setTextColor(Color.RED); 
    t3.setTypeface(null,Typeface.BOLD); 
    t3.setPadding(20,20,20,20); 
    tablerow.addView(t3); 

    t4 = new TextView(getApplicationContext()); 
    t4.setText("Gender"); 
    t4.setTextColor(Color.RED); 
    t4.setTypeface(null,Typeface.BOLD); 
    t4.setPadding(20,20,20,20); 
    tablerow.addView(t4); 

    tablelayout.addView(tablerow); 
    for(Integer j = 0;j<count;j++) 
    { 
     tablerow = new TableRow(getApplicationContext()); 

     t1 = new TextView(getApplicationContext()); 
     t1.setText(c.getString(c.getColumnIndex("first_name"))); 
     t1.setPadding(20, 20, 20, 20); 


     t2 = new TextView(getApplicationContext()); 
     t2.setText(c.getString(c.getColumnIndex("last_name"))); 
     t2.setPadding(20, 20, 20, 20); 

     t5 = new TextView(getApplicationContext()); 
     t5.setText(c.getString(c.getColumnIndex("userGender"))); 
     t5.setPadding(20, 20, 20, 20); 



     tablerow.addView(t1); 
     tablerow.addView(t2); 
     tablerow.addView(t5); 


     tablelayout.addView(tablerow); 

     c.moveToNext(); 
    } 
    setContentView(tablelayout); 
    pf.close(); 
} 

내가 선택한 라디오 버튼의 가치를 데이터베이스에 추가 할 수 있습니다. 나는 안드로이드 시뮬레이터와 프로그램을 실행할 때, 그것은 말한다 :

불행하게도, MyThirdApp이

이 사람이 나를 도울 수

이 중지되었습니다?

로그 캣 :이

10-02 06:36:51.319: E/SQLiteLog(1770): (1) near "INSER": syntax error 
10-02 06:36:51.399: E/AndroidRuntime(1770): FATAL EXCEPTION: main 
10-02 06:36:51.399: E/AndroidRuntime(1770): Process: com.example.mythirdapp, PID: 1770 
10-02 06:36:51.399: E/AndroidRuntime(1770): java.lang.IllegalStateException: Could not execute method of the activity 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.view.View$1.onClick(View.java:3823) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.view.View.performClick(View.java:4438) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.widget.CompoundButton.performClick(CompoundButton.java:100) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.view.View$PerformClick.run(View.java:18422) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.os.Handler.handleCallback(Handler.java:733) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.os.Handler.dispatchMessage(Handler.java:95) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.os.Looper.loop(Looper.java:136) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at java.lang.reflect.Method.invoke(Method.java:515) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at dalvik.system.NativeStart.main(Native Method) 
10-02 06:36:51.399: E/AndroidRuntime(1770): Caused by: java.lang.reflect.InvocationTargetException 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at java.lang.reflect.Method.invoke(Method.java:515) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.view.View$1.onClick(View.java:3818) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  ... 12 more 
10-02 06:36:51.399: E/AndroidRuntime(1770): Caused by: android.database.sqlite.SQLiteException: near "INSER": syntax error (code 1): , while compiling: INSER INTO Persone VALUES('female'); 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  at com.example.mythirdapp.MainActivity.RadioButtonClicked(MainActivity.java:56) 
10-02 06:36:51.399: E/AndroidRuntime(1770):  ... 15 more 

답변

0

안녕하지 않는 문제 :

첫째 추가

android:onClick="RadioButtonClicked" > 

당신의 남성과 여성 둘을 radioGroup 자식 요소 아래과 같이 :

android:id="@+id/radio_female" 
android:text="@string/radio_female" 
android:onClick="RadioButtonClicked" 

? 이제이 완료되어, 당신의 MainActivity에이 코드를 추가

//Note the name of the method must match the xml onClick value in this case 'RadioButtonClicked' 

public void RadioButtonClicked(View view) { 

//This variable will store whether the user was male or female 
String userGender=""; 
// Check that the button is now checked? 
boolean checked = ((RadioButton) view).isChecked(); 

// Check which radio button was clicked 
switch(view.getId()) { 
    case R.id.radio_female: 
     if (checked) 
      userGender = "female"; 
     break; 
    case R.id.radio_male: 
     if (checked) 
      userGender = "male"; 
     break; 
} 

//Now insert it into your database using userGender instead of gender 

pf.execSQL....+userGender 

}

+0

헤이, 그것은 당신의 컴퓨터에서 작업 않았다을 내가 노력하기 때문에, 그것이라고 작동하지 않았다 "불행하게도 ,, MyApp를했다 stopped " –

+0

이것은 작동하며 테스트를 거쳤습니다 ... 이메일을 보내 주시면 소스 코드를 보내 드리겠습니다. 참고 나는 단순히 결과를 기록 데이터베이스에 삽입하지 않았다. – Jonny2Plates

+0

오류 코드 logcat 출력을 붙여주세요. – Jonny2Plates

관련 문제