简体   繁体   中英

TextView not displaying final result

I am new to android , I creating an app for sql , its a simple app where user enters a name and the name appears on the display using a textview but unfortunately text is not displaying Here is the code:

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.example.ashis.mmm.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp" />
</RelativeLayout>

MainActivity.java

    package com.example.ashis.mmm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    Button button;
    TextView textView;
    Database db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=(EditText) findViewById(R.id.editText);
        button=(Button) findViewById(R.id.button);
        textView=(TextView) findViewById(R.id.textView);
        db = new Database(getApplicationContext(),null,null,1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String input = editText.getText().toString() ;
                Person person = new Person(input);
                db.add_row(person);
                editText.setText("");
                printDatabse();


            }
        });


    }

    private void printDatabse() {

        String  dbString = db.printData();
        textView.setText(dbString);

    }
}

Person.java

package com.example.ashis.mmm;

/**
 * Created by ashis on 8/27/2016.
 */
public class Person {

    private int _id;
    private String name;

    public Person() {


    }

    public Person(String name) {

        this.name = name;
    }


    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

DataBase.java

package com.example.ashis.mmm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by ashis on 8/27/2016.
 */
public class Database extends SQLiteOpenHelper {
    private static final int  DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "persons.db";
    private static final String TABLE_NAME = "Persons";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_NAME = "Name";

    public Database(Context context, String DATABASE_NAME, SQLiteDatabase.CursorFactory factory, int DATABASE_VERSION) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String onCreate = "CREATE TABLE " + TABLE_NAME + "( " +
                            COLUMN_ID + " INTEGER  ," +
                            COLUMN_NAME + " TEXT );";
        db.execSQL(onCreate);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }

    public void add_row(Person person)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, person.getName());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public String printData()
    {
        String dbqry = "";
        SQLiteDatabase db = getReadableDatabase();
        String printqry = "SELECT * FROM " + TABLE_NAME;

        Cursor cursor = db.rawQuery(printqry,null);
        cursor.moveToFirst();

        while (!cursor.isAfterLast())
        {
            if (cursor.getString(cursor.getColumnIndex("Name")) != null)
            {
                dbqry += cursor.getString(1);
                dbqry +="\n";

            }
        }

        db.close();
        cursor.close();

        return dbqry;
    }
}

the best way to see why the text is not appearing inside your TextView is to debug, but it seems that you are not so good to do it. In this case you can use Log.d(); method. I will suggest to you the places where you may have problems, just put some logs on those places and check what is happening with your variable and values.

  1. Check does your TextView is visible on the screen and does some other view is not cover it. You can do that for example by setting some background colour of the textview and some fixed width and height (50dp), this will show you the TextView.
  2. Inside printData() method put some logs to see what is coming from the DB, just to print your string values;
  3. Check the Cursor inside printData() just to be sure that it contains some records , maybe your DB name is wrong or DB table is wrong or something else.

If your view is visible on the screen and printData() method is working you will not have any other problems to see the text!

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