按下按钮后文本视图不显示

时间:2018-02-12 03:00:43

标签: java android sql database android-studio

我一直在努力解决这个问题,我不知道它为什么不起作用。

这是按下按钮后的输出示例

  1. 警告消息“详细显示成功!”出现。
  2. “John,Smith”出现在详细信息中。
  3. 这是我的MainActivity.java的代码

    package com.ite.database;
    
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.provider.Settings;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends ActionBarActivity {
    
    //Declaring variables
    SQLiteDatabase db1 = null;
    private static String DBNAME = "PEOPLE.db";
    TextView tvw;
    EditText edt1, edt2 = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Getting the controls
    
        edt1 = (EditText) findViewById(R.id.edt1);
        edt2 = (EditText) findViewById(R.id.edt2);
        tvw = (TextView) findViewById(R.id.tvw4);
    
        //Database will be created through below method
        db1 = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
    
    }
    
    public void btnSubmit(View view) {
    
        //For fetching data from EditText, use editable instead of string
    
        try {
    
            db1.execSQL("CREATE TABLE IF NOT EXIST PeopleTable(ID INTEGER PRIMARY KEY, FIRSTNAME VARCHAR, LASTNAME VARCHAR);");
            db1.execSQL("INSERT INTO PeopleTable(FIRSTNAME, LASTNAME) VALUES (" + edt1.getText() + "," + edt2.getText() + ");");
    
            Cursor c = db1.rawQuery("SELECT*FROM PeopleTable", null);
    
            if (c != null) {
    
                if (c.moveToFirst()) {
    
                    do {
    
                        //Whole data of column is fetched by getColumnIndex()
                        String firstname = c.getString(c.getColumnIndex("First Name"));
                        String lastname = c.getString(c.getColumnIndex("Last Name"));
                        //Debugging purpose
    
                        System.out.println(firstname);
                        System.out.println(lastname);
    
                    } while (c.moveToNext());
    
                }
    
                //count the total number of entries
                Integer a = c.getCount();
                System.out.println(a);
                //db1.close();
                //if you close the database then illegal exception will be occured...
    
            }
        } catch (Exception e) {
    
            System.out.println(e);
    
        }
    
        //---display file saved message---|
        Toast.makeText(getBaseContext(), "Submit successfully!", Toast.LENGTH_SHORT).show();
    
    }
    
    public void btnDetail(View view) {
    
        String sData = "";
        try {
    
            Cursor c = db1.rawQuery("SELECT*FROM PeopleTable", null);
    
            if (c != null) {
    
                if (c.moveToFirst()) {
    
                    do {
    
                        //Whole data of column is fetched by getColumnIndex()
                        String firstname = c.getString(c.getColumnIndex("First Name"));
                        String lastname = c.getString(c.getColumnIndex("Last Name"));
    
                        System.out.println(firstname);
                        System.out.println(lastname);
    
                        sData += firstname + "," + lastname + "\n";
    
                    } while (c.moveToNext());
                }
    
                //Count the total number of entries
                Integer a = c.getCount();
                System.out.println(a);
                //db1.close();
                //If you close the database then illegal exception will be occured...
    
            }
        } catch (Exception e) {
    
            System.out.println(e);
    
        }
    
        tvw.setText(sData);
        System.out.print(sData);
    
        //---display file saved message---
        Toast.makeText(getBaseContext(), "Details shown successfully!", Toast.LENGTH_SHORT).show();
    
    }
    
    public void btnDeleteAll(View view) {
    
        db1.execSQL("DELTE FROM PeopleTable WHERE ID > -1;");
    
        //---display file saved message---
    
        Toast.makeText(getBaseContext(), "Delete All Successfully!", Toast.LENGTH_SHORT).show();
    
    }
    
    public void btnUpdate(View view){
    
        db1.execSQL("UPDATE PeopleTable SET FIRSTNAME = "+ edt1.getText() + " WHERE LASTNAME = " + edt2.getText() + "");
    
        //---display file saved message---
    
        Toast.makeText(getBaseContext(), "Update successfully!", Toast.LENGTH_SHORT).show();
    
    }
    }
    

    这是我的activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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.ite.database.MainActivity">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Registration Form"
        android:id="@+id/tvw1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Name"
        android:id="@+id/tvw2"
        android:layout_below="@+id/tvw1"
        android:layout_centerHorizontal="true" />
    
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edt1"
        android:layout_below="@+id/tvw2"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/edt2" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Last Name"
        android:id="@+id/tvw3"
        android:layout_below="@+id/edt1"
        android:layout_centerHorizontal="true" />
    
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edt2"
        android:layout_below="@+id/tvw3"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create Record"
        android:id="@+id/button1"
        android:layout_below="@+id/edt2"
        android:layout_alignEnd="@+id/edt2"
        android:layout_alignParentStart="true"
        android:onClick="btnSubmit"/>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Detail"
        android:id="@+id/button2"
        android:layout_below="@+id/button1"
        android:layout_alignEnd="@+id/button1"
        android:layout_alignParentStart="true"
        android:onClick="btnDetail"/>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete All"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/button2"
        android:onClick="btnDeleteAll"/>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Update First Name"
        android:id="@+id/button4"
        android:layout_below="@+id/button3"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/button3"
        android:onClick="btnUpdate"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Details"
        android:id="@+id/tvw4"
        android:layout_below="@+id/button4"
        android:layout_centerHorizontal="true" />
    </RelativeLayout>
    

1 个答案:

答案 0 :(得分:0)

初看起来,您的查询字符串格式错误。

此查询

 Cursor c = db1.rawQuery("SELECT*FROM PeopleTable", null);

应该是

 Cursor c = db1.rawQuery("SELECT * FROM PeopleTable", null);

同时更改此查询

Cursor c = db1.rawQuery("SELECT*FROM PeopleTable", null);

Cursor c = db1.rawQuery("SELECT * FROM PeopleTable", null);

如果其他事情没问题,那么这可以解决您的问题。