簡體   English   中英

按下按鈕后未顯示文本視圖

[英]Text View not showing after pressing a button

我已經為這個問題苦苦掙扎了好幾天,我也不知道為什么它不起作用。

這是按下按鈕后的輸出示例

  1. 出現警報消息“成功顯示詳細信息!”。
  2. “ John,Smith”出現在“詳細信息”中。

這是我的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>

乍一看,您的查詢字符串格式錯誤。

這個查詢

 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);

如果沒有其他問題,那么可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM