简体   繁体   中英

Text View not showing after pressing a button

I have been struggling this problem for days and I have no idea why is it not working .

This is an example of an output after pressing the button

  1. Alert Message “Details Shown Successfully!” appears.
  2. “John, Smith” appears at the Details.

This is the code of my 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();

}
}

This is my 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>

At first look, your query string are in wrong format.

This query

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

should be

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

Also change this query

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

to

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

If other things are ok then this may solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM