如何使用EditText将数据插入数据库,然后将数据保存到android中的数据库中

时间:2012-08-28 11:15:14

标签: android sqlite

我是android开发的新手,所以我正在尝试但我不能使用编辑文本将数据插入数据库,然后使用Android中的Button将数据保存到数据中。 所以有人请帮助我,我只是一个初学者,这就是为什么请写一个示例教程/程序。

高级谢谢你, arnob

好的,这是我的一个类活动和我附加的xml文件 -

我的一个活动类名称“Registration.java”,它包含 -

package com.ggit.trip.caster;

  public class Registration extends AppBaseActivity {

private Button submit3;
private Button exit3;

DatePicker d;
Dialog dialog;
public TextView date;
private ImageView cal;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.registration);

    registerBaseActivityReceiver();

    submit3 = (Button) findViewById(R.id.button1);
    exit3 = (Button) findViewById(R.id.button2);

    cal = (ImageView) findViewById(R.id.imageView1);
    date = (TextView) findViewById(R.id.textView8);

    submit3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getApplicationContext(),
                    CostManagement.class);
            startActivity(intent);

        }
    });

    cal.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog = new Dialog(Registration.this);
            dialog.setContentView(R.layout.date);
        //dialog.setTitle("Set Date");
            dialog.setCancelable(true);

            Button ok = (Button) dialog.findViewById(R.id.button1);
            Button cancel = (Button) dialog.findViewById(R.id.button2);

            d = (DatePicker) dialog.findViewById(R.id.datePicker1);

            ok.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int day = d.getDayOfMonth();
                    int month = d.getMonth();
                    int year = d.getYear();

            String curDate = "" + day + "/" + month + "/" + year;

                    Log.d("date", "" + curDate);

                    date.setText(curDate);
                    date.setVisibility(View.VISIBLE);
                    cal.setVisibility(View.INVISIBLE);

                    dialog.dismiss();
                }
            });
            cancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.dismiss();
                }           
            });     

            dialog.show();
        }
    });

    exit3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            closeAllActivities();
        }
    });
}
@Override
public void onDestroy() {

    super.onDestroy();
    unRegisterBaseActivityReceiver();
}

}

对于这个类活动,我为这个名为“registration.xml”的注册做了一个布局。

    <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:background="@drawable/sea_01" >

<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="30dp"
    android:text="User Name"
    android:textAppearance="?android:attr/textAppearanceMedium"
     />
<EditText
    android:id="@+id/editText1"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:layout_marginRight="24dp"
    android:background="@drawable/rounded_edittext"
    android:ems="10"
    android:hint="  insert your name"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="20dp"
    android:text="Passord"
    android:textAppearance="?android:attr/textAppearanceMedium"
     />
<EditText
    android:id="@+id/editText2"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/editText1"
    android:background="@drawable/rounded_edittext"
    android:ems="10"
    android:inputType="textPassword" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="20dp"
    android:text="E-mail"
    android:textAppearance="?android:attr/textAppearanceMedium"
     />

<EditText
    android:id="@+id/editText3"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/editText2"
    android:background="@drawable/rounded_edittext"
    android:ems="10"
    android:hint="  insert your email"
    android:inputType="textEmailAddress" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="20dp"
    android:text="Phone Number"
    android:textAppearance="?android:attr/textAppearanceMedium"
     />

<EditText
    android:id="@+id/editText4"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView4"
    android:layout_alignBottom="@+id/textView4"
    android:layout_alignRight="@+id/editText3"
    android:background="@drawable/rounded_edittext"
    android:ems="10"
    android:hint="   contact number"
    android:inputType="phone" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText4"
    android:layout_marginTop="32dp"
    android:text="Gender"
    android:textAppearance="?android:attr/textAppearanceMedium"
     />

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView5"
    android:layout_alignBottom="@+id/textView5"
    android:layout_alignLeft="@+id/editText4"
    android:text="Male"
     />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radioButton1"
    android:layout_alignBottom="@+id/radioButton1"
    android:layout_toRightOf="@+id/radioButton1"
    android:text="Female"
     />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/radioButton1"
    android:layout_marginTop="26dp"
    android:text="Location"
    android:textAppearance="?android:attr/textAppearanceMedium"
     />

<EditText
    android:id="@+id/editText5"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView7"
    android:layout_alignBottom="@+id/textView7"
    android:layout_alignLeft="@+id/radioButton1"
    android:background="@drawable/rounded_edittext"
    android:ems="10"
    android:hint="   place you living" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText5"
    android:layout_marginTop="32dp"
    android:text="Birthday"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText5"
    android:layout_alignTop="@+id/textView6"
    android:src="@drawable/calendar" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView6"
    android:layout_alignLeft="@+id/imageView1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:visibility="invisible" />

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="45dp"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/imageView1"
    android:background="@drawable/gloss_nine_patch"
    android:text="Submit" />

<Button
    android:id="@+id/button2"
    android:layout_width="100dp"
    android:layout_height="45dp"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="24dp"
    android:background="@drawable/gloss_nine_patch"
    android:text="Exit" />


<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="User Registration"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="12dp" />

  </RelativeLayout>

现在请有人请使用数据库查询完全填写此“Registration.java”类,在我点击“提交”按钮后写入所有信息后,它将保存到数据库中,然后转到其他活动。

感谢大家 请帮帮我吧

3 个答案:

答案 0 :(得分:4)

Arnob你可以在以下链接找到非常好的教程,......

http://www.vogella.com/articles/AndroidSQLite/article.html

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

我希望它对你有所帮助。

答案 1 :(得分:1)

Android SDK平台提供了一个记事本SDK示例,可以完成所有这些操作。你可以通过那个。此外,有数百个关于使用Android应用程序的数据库免费提供的教程。

答案 2 :(得分:0)

你可能需要像

这样的东西
public class AndroidSQLiteTutorialActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    DatabaseHandler db = new DatabaseHandler(this);
    EditText getTextFromThis = (EditText) findViewById(getTextFromThis);
    EditText putTextHere = (EditText) findViewById(putTextHere);
    Button b1 = (Button) findViewById(R.id.myButton);
      b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          String s = getTextFromHere.getText().toString(); //getting confused where you get/put the data.
          db.add(new StringObjectThingy(s)); //say you have a SQLiteDB with StringObjectThingies
          putTextHere.setText(s);
        }
      }
    }
  });
}

这更好吗?