0

I am making an app in which I am using sqlite Database for generating random values, but it is not giving the value. It always shows a blank Activity even not showing the added record. Here is my code. I want to display some random data in a ListView.

I have another class by which I insert the data and there is a Button to show the data and a function to call an Activity which generates some random data, but that Activity is not properly working

import android.content.Context;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;


public class ViewPeople extends AppCompatActivity {


    private static final String SELECT_SQL = "SELECT * FROM persons";
    private Cursor mCurRandom;
    private SQLiteDatabase db;

    private Cursor c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_people);
        openDatabase();
        c = db.rawQuery(SELECT_SQL, null);
        c.moveToFirst();
        showRecords();
    }

    protected void openDatabase() {
        db = openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);
    }

    protected void showRecords() {

        try {
            String sql = "SELECT * FROM persons ORDER BY RANDOM() LIMIT 1";

            mCurRandom = db.rawQuery(sql, null);
            if (mCurRandom != null) {
                mCurRandom.moveToNext();
                return;
            }
            {

                showRecords();

            }


        } catch (SQLException mSQLException) {

            throw mSQLException;
        }
        return;
    }

}

My layout is

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".ViewPeople">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/textViewName" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </ListView>
    </LinearLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • _"but that Activity is not properly working"_ - Can you give us a clue what that means? In what way is it not working properly? – Ted Hopp Dec 11 '16 at 18:30
  • 1
    It shows nothing not even all the records which are added in database – Gaurav Awasthi Dec 11 '16 at 18:33
  • You need to populate the ListView via an Adapter. Perhaps SimpleCursorAdapter or a Custom Adapter. Also note the answer re logic error and showRecords calling itself. Have a look at this [Android: Using SimpleCursorAdapter to get Data from Database to ListView](http://stackoverflow.com/questions/12077955/android-using-simplecursoradapter-to-get-data-from-database-to-listview). However, you will only ever get 1 record (unless you change limit) so no need for a ListView with LIMIT 1. – MikeT Dec 11 '16 at 18:45

1 Answers1

1

It looks like you have a logic error:

mCurRandom = db.rawQuery(sql, null);
if (mCurRandom != null) {
    mCurRandom.moveToNext();
    return;
}
{

    showRecords();

}

First, of all, db.rawQuery will never return null; it will either return a Cursor object or throw an exception. So the branch is always taken. When that happens, showRecords() is not called.

And that's probably a good thing, because otherwise you'd have an infinite recursion, with showRecords() calling itself over and over.

This seems opposite of what you mean. What you probably want is:

if (mCurRandom.moveToFirst()) {
    // Do something with the random record
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • @GauravAwasthi - It's not apparent from your code what you are trying to accomplish. Is the activity supposed to display a single random record in the `TextView`? Is it supposed to display the entire data base contents in the `ListView`? – Ted Hopp Dec 11 '16 at 18:47
  • Simple one random record i want to dispaly – Gaurav Awasthi Dec 11 '16 at 18:49
  • @GauravAwasthi -- Just replace `// Do something..` with the code necessary to retrieve a column value from the `Cursor` and then call the field's `setText()` method. (You might want to just retrieve the column of interest in the `Cursor` instead of retrieving all columns with `Select *`.) – Ted Hopp Dec 11 '16 at 18:54