-2

我是 android 开发的新手,所以我正在尝试,但我无法使用 Edit Text 将数据插入数据库,然后使用 android 中的 Button 将数据保存到数据中。所以有人请帮帮我,我只是一个初学者,所以请在上面写一个示例教程/程序。

先进的谢谢你,阿诺布

好的,这是我的一项课堂活动和我附上的一个 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”类,在写完所有信息后,当我单击“提交”按钮时,它将保存到数据库中,然后转到其他活动。

谢谢大家现在请帮助我

4

3 回答 3

4

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

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

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

我希望它对你有帮助。

于 2012-08-28T11:30:13.380 回答
1

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

于 2012-08-28T11:20:52.763 回答
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);
        }
      }
    }
  });
}

这个更好?

于 2012-08-28T11:26:15.527 回答