Android数据库数据检索

时间:2013-09-01 19:15:59

标签: android database android-tablelayout

我开发了一个Android应用程序...用户可以在其中输入名字,姓氏和电子邮件......当按下“创建”按钮时,用户输入的值将在数据库中更新...当另一个按钮时按下“显示数据”,显示更新的数据并且应用程序工作正常...但我的问题是当用户按下另一个名为“检索”的按钮时,所有更新的数据必须与显示数据一样显示。 ..但是当用户点击他需要的数据时...它必须在他们的确切位置“firstname”姓氏和电子邮件中上传到第一页....因为我必须做的事情请帮助...我我在这里提供我开发的所有代码..请仔细阅读并帮助...

XML ---数据

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <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_marginLeft="27dp"
        android:layout_marginTop="86dp"
        android:text="First Name" />

    <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="54dp"
        android:text="Last Name" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_centerVertical="true"
        android:text="E-Mail" />

    <EditText
        android:id="@+id/firstName"
        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:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="80dp"
        android:onClick="Adddata"
        android:text="Create" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="26dp"
        android:onClick="close"
        android:text="Close" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:onClick="Showdata"
        android:text="Show Data" />

    <EditText
        android:id="@+id/lastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/eMail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="textPersonName" />

</RelativeLayout>

mainactivity

public class MainActivity extends Activity {

    String fname, lname, email;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) 

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = openOrCreateDatabase("MyDBI", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Student (fname VARCHAR, lname VARCHAR, email VARCHAR);");



    }

      public void Adddata(View view)
      {
          EditText editText1 = (EditText) findViewById (R.id.firstName);
          EditText editText2 = (EditText) findViewById (R.id.lastName);
          EditText editText3 = (EditText) findViewById (R.id.eMail);

          fname = editText1.getText().toString();
          lname = editText2.getText().toString();
          email = editText3.getText().toString();


          db.execSQL("INSERT INTO STUDENT VALUES('"+fname+"', '"+lname+"', '"+email+"');");
      }



      public void Showdata(View view)
      {
          Cursor c = db.rawQuery("SELECT * from Student", null);
          int count = c.getCount();
          c.moveToFirst();

          TableLayout tableLayout = new TableLayout(getApplicationContext());
          tableLayout.setVerticalScrollBarEnabled(true);

          TableRow tableRow;

          TextView textView, textView1, textView2, textView3, textView4, textView5;

          tableRow = new TableRow(getApplicationContext());
          textView = new TextView(getApplicationContext());

          textView.setText("FirstName");
          textView.setTextColor(Color.RED);
          textView.setTypeface(null, Typeface.BOLD);
          textView.setPadding(20, 20, 20, 20);

          tableRow.addView(textView);

          textView4 = new TextView(getApplicationContext());
          textView4.setText("LastName");
          textView4.setTextColor(Color.RED);
          textView4.setTypeface(null, Typeface.BOLD);
          textView4.setPadding(20, 20, 20, 20);

          tableRow.addView(textView4);

          textView5 = new TextView(getApplicationContext());
          textView5.setText("Email");
          textView5.setTextColor(Color.RED);
          textView5.setTypeface(null, Typeface.BOLD);
          textView5.setPadding(20, 20, 20, 20);

          tableRow.addView(textView5);

          tableLayout.addView(tableRow);

          for(Integer j=0; j<count; j++)

          {
              tableRow = new TableRow(getApplicationContext());

              textView1 = new TextView(getApplicationContext());
              textView1.setText(c.getString(c.getColumnIndex("fname")));

              textView2 = new TextView(getApplicationContext());
              textView2.setText(c.getString(c.getColumnIndex("lname")));

              textView3 = new TextView(getApplicationContext());
              textView3.setText(c.getString(c.getColumnIndex("email")));

              textView1.setPadding(20, 20, 20, 20);
              textView2.setPadding(20, 20, 20, 20);
              textView3.setPadding(20, 20, 20, 20);

              tableRow.addView(textView1);
              tableRow.addView(textView2);
              tableRow.addView(textView3);

              tableLayout.addView(tableRow);
              c.moveToNext();

          }



          setContentView(tableLayout);
          db.close();





      }

      public void close (View view)
      {
          System.exit(0);
      }
    }

0 个答案:

没有答案