Gaurav Awasthi
Gaurav Awasthi

Reputation: 5

Unable to get some random data form SQLite

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>

Upvotes: 0

Views: 69

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234795

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
}

Upvotes: 1

Related Questions