SQLite Database in ListView Android












-1















Having a problem with displaying some records from a SQLite database in a List View in android studio. The code for displaying the list view is:



import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class PreviousHunts extends AppCompatActivity implements View.OnClickListener
{
public TextView txtDate;
public String strDate = "";
public ListView lstView;

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

Button btnDateGet = findViewById(R.id.btnGetHunts);
btnDateGet.setOnClickListener(this);

Button btnHome = findViewById(R.id.btnHuntsHome);
btnHome.setOnClickListener(this);

txtDate = findViewById(R.id.txtDateThing);
lstView = findViewById(R.id.lstView);
}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnGetHunts:
getDate();
break;
case R.id.btnHuntsHome:
goHome();
break;
}
}

private void getDate()
{
final Calendar c = Calendar.getInstance();
final int Year = c.get(Calendar.YEAR);
final int Month = c.get(Calendar.MONTH);
final int Day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
String day = String.valueOf(dayOfMonth);
String Smonth = String.valueOf(month + 1);
String sYear = String.valueOf(year);

txtDate.setText(day + "/" + Smonth + "/" + sYear);
strDate = day + "/" + Smonth + "/" + sYear;
}
},Year, Month, Day);
datePickerDialog.show();

DatabaseHelper dbHelp = new DatabaseHelper(this);
int huntID = dbHelp.getHuntIDforPastHunts(strDate);

if (huntID == 0)
{
Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
}
else
{
LastPart(huntID);
}
}

private void LastPart(int huntID)
{
DatabaseHelper dbHelp = new DatabaseHelper(this);
Cursor cursor = dbHelp.getPastHuntLogs(huntID);

cursor.moveToFirst();

//SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, lstView, cursor, new String {"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot",
//"OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"}, new int {100}, 0);

//lstView.setAdapter(adapter);

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.activity_previous_hunts,
cursor,
new String{"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot", "OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"},
new int{1, 2, 3, 4, 5, 6}
,0);

lstView.setAdapter(adapter);
}

private void goHome()
{
Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
startActivity(intent);
finish();
}
}

The database helper code is:

public Cursor getPastHuntLogs(int huntID)
{
String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
"AnimalShotNameORTypeClaysShot FROM Log WHERE HuntID = " + "'huntID'";
Cursor cursor = db.rawQuery(query, null);
return cursor;
}


XML for the listview, etc, is:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PreviousHunts">

<Button
android:id="@+id/btnGetHunts"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:text="Select Date you wish to see hunts from"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtDateThing"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginEnd="100dp"
android:layout_marginTop="45dp"
android:text="Date"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnHuntsHome"
android:layout_width="88dp"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="100dp"
android:layout_marginStart="100dp"
android:text="Home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ListView
android:id="@+id/lstView"
android:layout_width="362dp"
android:layout_height="342dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGetHunts" />
</android.support.constraint.ConstraintLayout>


The problem is that nothing happens. There are no errors in the run log and nothing is displaying. Help would be really appreciated.










share|improve this question

























  • check your adapter is null or not.

    – Android Team
    Nov 23 '18 at 9:13











  • @AndroidTeam how would one check if the adapter is null?

    – Alexander Mcmichael
    Nov 23 '18 at 9:15






  • 2





    The DatePickerDialog you're showing in getDate() does not pause execution in that method. It will immediately continue to your database operations, and with strDate not set, I would imagine that you're not getting any records back in the returned Cursor.

    – Mike M.
    Nov 23 '18 at 9:19


















-1















Having a problem with displaying some records from a SQLite database in a List View in android studio. The code for displaying the list view is:



import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class PreviousHunts extends AppCompatActivity implements View.OnClickListener
{
public TextView txtDate;
public String strDate = "";
public ListView lstView;

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

Button btnDateGet = findViewById(R.id.btnGetHunts);
btnDateGet.setOnClickListener(this);

Button btnHome = findViewById(R.id.btnHuntsHome);
btnHome.setOnClickListener(this);

txtDate = findViewById(R.id.txtDateThing);
lstView = findViewById(R.id.lstView);
}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnGetHunts:
getDate();
break;
case R.id.btnHuntsHome:
goHome();
break;
}
}

private void getDate()
{
final Calendar c = Calendar.getInstance();
final int Year = c.get(Calendar.YEAR);
final int Month = c.get(Calendar.MONTH);
final int Day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
String day = String.valueOf(dayOfMonth);
String Smonth = String.valueOf(month + 1);
String sYear = String.valueOf(year);

txtDate.setText(day + "/" + Smonth + "/" + sYear);
strDate = day + "/" + Smonth + "/" + sYear;
}
},Year, Month, Day);
datePickerDialog.show();

DatabaseHelper dbHelp = new DatabaseHelper(this);
int huntID = dbHelp.getHuntIDforPastHunts(strDate);

if (huntID == 0)
{
Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
}
else
{
LastPart(huntID);
}
}

private void LastPart(int huntID)
{
DatabaseHelper dbHelp = new DatabaseHelper(this);
Cursor cursor = dbHelp.getPastHuntLogs(huntID);

cursor.moveToFirst();

//SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, lstView, cursor, new String {"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot",
//"OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"}, new int {100}, 0);

//lstView.setAdapter(adapter);

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.activity_previous_hunts,
cursor,
new String{"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot", "OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"},
new int{1, 2, 3, 4, 5, 6}
,0);

lstView.setAdapter(adapter);
}

private void goHome()
{
Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
startActivity(intent);
finish();
}
}

The database helper code is:

public Cursor getPastHuntLogs(int huntID)
{
String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
"AnimalShotNameORTypeClaysShot FROM Log WHERE HuntID = " + "'huntID'";
Cursor cursor = db.rawQuery(query, null);
return cursor;
}


XML for the listview, etc, is:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PreviousHunts">

<Button
android:id="@+id/btnGetHunts"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:text="Select Date you wish to see hunts from"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtDateThing"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginEnd="100dp"
android:layout_marginTop="45dp"
android:text="Date"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnHuntsHome"
android:layout_width="88dp"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="100dp"
android:layout_marginStart="100dp"
android:text="Home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ListView
android:id="@+id/lstView"
android:layout_width="362dp"
android:layout_height="342dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGetHunts" />
</android.support.constraint.ConstraintLayout>


The problem is that nothing happens. There are no errors in the run log and nothing is displaying. Help would be really appreciated.










share|improve this question

























  • check your adapter is null or not.

    – Android Team
    Nov 23 '18 at 9:13











  • @AndroidTeam how would one check if the adapter is null?

    – Alexander Mcmichael
    Nov 23 '18 at 9:15






  • 2





    The DatePickerDialog you're showing in getDate() does not pause execution in that method. It will immediately continue to your database operations, and with strDate not set, I would imagine that you're not getting any records back in the returned Cursor.

    – Mike M.
    Nov 23 '18 at 9:19
















-1












-1








-1








Having a problem with displaying some records from a SQLite database in a List View in android studio. The code for displaying the list view is:



import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class PreviousHunts extends AppCompatActivity implements View.OnClickListener
{
public TextView txtDate;
public String strDate = "";
public ListView lstView;

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

Button btnDateGet = findViewById(R.id.btnGetHunts);
btnDateGet.setOnClickListener(this);

Button btnHome = findViewById(R.id.btnHuntsHome);
btnHome.setOnClickListener(this);

txtDate = findViewById(R.id.txtDateThing);
lstView = findViewById(R.id.lstView);
}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnGetHunts:
getDate();
break;
case R.id.btnHuntsHome:
goHome();
break;
}
}

private void getDate()
{
final Calendar c = Calendar.getInstance();
final int Year = c.get(Calendar.YEAR);
final int Month = c.get(Calendar.MONTH);
final int Day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
String day = String.valueOf(dayOfMonth);
String Smonth = String.valueOf(month + 1);
String sYear = String.valueOf(year);

txtDate.setText(day + "/" + Smonth + "/" + sYear);
strDate = day + "/" + Smonth + "/" + sYear;
}
},Year, Month, Day);
datePickerDialog.show();

DatabaseHelper dbHelp = new DatabaseHelper(this);
int huntID = dbHelp.getHuntIDforPastHunts(strDate);

if (huntID == 0)
{
Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
}
else
{
LastPart(huntID);
}
}

private void LastPart(int huntID)
{
DatabaseHelper dbHelp = new DatabaseHelper(this);
Cursor cursor = dbHelp.getPastHuntLogs(huntID);

cursor.moveToFirst();

//SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, lstView, cursor, new String {"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot",
//"OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"}, new int {100}, 0);

//lstView.setAdapter(adapter);

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.activity_previous_hunts,
cursor,
new String{"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot", "OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"},
new int{1, 2, 3, 4, 5, 6}
,0);

lstView.setAdapter(adapter);
}

private void goHome()
{
Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
startActivity(intent);
finish();
}
}

The database helper code is:

public Cursor getPastHuntLogs(int huntID)
{
String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
"AnimalShotNameORTypeClaysShot FROM Log WHERE HuntID = " + "'huntID'";
Cursor cursor = db.rawQuery(query, null);
return cursor;
}


XML for the listview, etc, is:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PreviousHunts">

<Button
android:id="@+id/btnGetHunts"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:text="Select Date you wish to see hunts from"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtDateThing"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginEnd="100dp"
android:layout_marginTop="45dp"
android:text="Date"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnHuntsHome"
android:layout_width="88dp"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="100dp"
android:layout_marginStart="100dp"
android:text="Home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ListView
android:id="@+id/lstView"
android:layout_width="362dp"
android:layout_height="342dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGetHunts" />
</android.support.constraint.ConstraintLayout>


The problem is that nothing happens. There are no errors in the run log and nothing is displaying. Help would be really appreciated.










share|improve this question
















Having a problem with displaying some records from a SQLite database in a List View in android studio. The code for displaying the list view is:



import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class PreviousHunts extends AppCompatActivity implements View.OnClickListener
{
public TextView txtDate;
public String strDate = "";
public ListView lstView;

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

Button btnDateGet = findViewById(R.id.btnGetHunts);
btnDateGet.setOnClickListener(this);

Button btnHome = findViewById(R.id.btnHuntsHome);
btnHome.setOnClickListener(this);

txtDate = findViewById(R.id.txtDateThing);
lstView = findViewById(R.id.lstView);
}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnGetHunts:
getDate();
break;
case R.id.btnHuntsHome:
goHome();
break;
}
}

private void getDate()
{
final Calendar c = Calendar.getInstance();
final int Year = c.get(Calendar.YEAR);
final int Month = c.get(Calendar.MONTH);
final int Day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
String day = String.valueOf(dayOfMonth);
String Smonth = String.valueOf(month + 1);
String sYear = String.valueOf(year);

txtDate.setText(day + "/" + Smonth + "/" + sYear);
strDate = day + "/" + Smonth + "/" + sYear;
}
},Year, Month, Day);
datePickerDialog.show();

DatabaseHelper dbHelp = new DatabaseHelper(this);
int huntID = dbHelp.getHuntIDforPastHunts(strDate);

if (huntID == 0)
{
Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
}
else
{
LastPart(huntID);
}
}

private void LastPart(int huntID)
{
DatabaseHelper dbHelp = new DatabaseHelper(this);
Cursor cursor = dbHelp.getPastHuntLogs(huntID);

cursor.moveToFirst();

//SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, lstView, cursor, new String {"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot",
//"OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"}, new int {100}, 0);

//lstView.setAdapter(adapter);

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.activity_previous_hunts,
cursor,
new String{"BirdName", "SeenORShot", "BirdAge", "NumSeenORShot", "OtherAnimalShotORClayShoot", "AnimalShotNameORTypeClaysShot"},
new int{1, 2, 3, 4, 5, 6}
,0);

lstView.setAdapter(adapter);
}

private void goHome()
{
Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
startActivity(intent);
finish();
}
}

The database helper code is:

public Cursor getPastHuntLogs(int huntID)
{
String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
"AnimalShotNameORTypeClaysShot FROM Log WHERE HuntID = " + "'huntID'";
Cursor cursor = db.rawQuery(query, null);
return cursor;
}


XML for the listview, etc, is:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PreviousHunts">

<Button
android:id="@+id/btnGetHunts"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:text="Select Date you wish to see hunts from"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtDateThing"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginEnd="100dp"
android:layout_marginTop="45dp"
android:text="Date"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnHuntsHome"
android:layout_width="88dp"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="100dp"
android:layout_marginStart="100dp"
android:text="Home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ListView
android:id="@+id/lstView"
android:layout_width="362dp"
android:layout_height="342dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGetHunts" />
</android.support.constraint.ConstraintLayout>


The problem is that nothing happens. There are no errors in the run log and nothing is displaying. Help would be really appreciated.







android sqlite simplecursoradapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:53









Aniruddh Parihar

2,21911129




2,21911129










asked Nov 23 '18 at 9:11









Alexander McmichaelAlexander Mcmichael

267




267













  • check your adapter is null or not.

    – Android Team
    Nov 23 '18 at 9:13











  • @AndroidTeam how would one check if the adapter is null?

    – Alexander Mcmichael
    Nov 23 '18 at 9:15






  • 2





    The DatePickerDialog you're showing in getDate() does not pause execution in that method. It will immediately continue to your database operations, and with strDate not set, I would imagine that you're not getting any records back in the returned Cursor.

    – Mike M.
    Nov 23 '18 at 9:19





















  • check your adapter is null or not.

    – Android Team
    Nov 23 '18 at 9:13











  • @AndroidTeam how would one check if the adapter is null?

    – Alexander Mcmichael
    Nov 23 '18 at 9:15






  • 2





    The DatePickerDialog you're showing in getDate() does not pause execution in that method. It will immediately continue to your database operations, and with strDate not set, I would imagine that you're not getting any records back in the returned Cursor.

    – Mike M.
    Nov 23 '18 at 9:19



















check your adapter is null or not.

– Android Team
Nov 23 '18 at 9:13





check your adapter is null or not.

– Android Team
Nov 23 '18 at 9:13













@AndroidTeam how would one check if the adapter is null?

– Alexander Mcmichael
Nov 23 '18 at 9:15





@AndroidTeam how would one check if the adapter is null?

– Alexander Mcmichael
Nov 23 '18 at 9:15




2




2





The DatePickerDialog you're showing in getDate() does not pause execution in that method. It will immediately continue to your database operations, and with strDate not set, I would imagine that you're not getting any records back in the returned Cursor.

– Mike M.
Nov 23 '18 at 9:19







The DatePickerDialog you're showing in getDate() does not pause execution in that method. It will immediately continue to your database operations, and with strDate not set, I would imagine that you're not getting any records back in the returned Cursor.

– Mike M.
Nov 23 '18 at 9:19














3 Answers
3






active

oldest

votes


















0














Try adding database code after date selection.



Like this



private void getDate() {
final Calendar c = Calendar.getInstance();
final int Year = c.get(Calendar.YEAR);
final int Month = c.get(Calendar.MONTH);
final int Day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
String day = String.valueOf(dayOfMonth);
String Smonth = String.valueOf(month + 1);
String sYear = String.valueOf(year);

txtDate.setText(day + "/" + Smonth + "/" + sYear);
strDate = day + "/" + Smonth + "/" + sYear;

// Try database code here
DatabaseHelper dbHelp = new DatabaseHelper(this);
int huntID = dbHelp.getHuntIDforPastHunts(strDate);

if (huntID == 0)
{
Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
}
else
{
LastPart(huntID);
}

}
},Year, Month, Day);
datePickerDialog.show();
}





share|improve this answer
























  • It didn't work unfortunately, no change in any display. Thanks for trying though.

    – Alexander Mcmichael
    Nov 23 '18 at 9:32











  • @AlexanderMcmichael can you make sure that query is correct? Or are there any values coming from your DB?

    – ahuja007
    Nov 23 '18 at 10:28





















0














The main issue you have is that you aren't invoking the population of the list after you have retrieved the date. Rather that the list view is being populated before the date has been retrieved.



That is the code :-



    DatabaseHelper dbHelp = new DatabaseHelper(this);
int huntID = dbHelp.getHuntIDforPastHunts(strDate);

if (huntID == 0)
{
Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
}
else
{
LastPart(huntID);
}


will execute before you have got the date, so no date no data extracted.



There are other issues, such as each time you hit the button you create a new adapter, when you should use one and refresh the list.



The following is how I'd suggest doing it and it's working code and has been tested. However, for the testing I've had to guess much of the DatabaseHelper as all that was available was the getPastHuntLogs method.



DatabaseHelper.java used was :-



public class DatabaseHelper extends SQLiteOpenHelper {

/**
* Suggest using 1 definitaion of table and column names and to ALWAYS use the constants
*/
public static final String DBNAME = "hunter";
public static final int DBVERSION = 1;
public static final String TABLE_LOG = "Log";
public static final String COL_TIMESTAMP = "timestamp";
public static final String COl_LOG_HUNTID = "HuntID";
public static final String COL_LOG_BIRDNAME = "BirdName";
public static final String COL_LOG_SEENORSHOT = "SeenOrShot";
public static final String COL_LOG_BIRDAGE = "BirdAge";
public static final String COL_LOG_NUMSEENORSHOT = "NumSeenOrShot";
public static final String COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT = "OtherAnimalShotORClayShoot";
public static final String COL_LOG_ANIMALSHORORTYPECLAYSHOOT = "AnimalShotNameORTypeClaysShot";

SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
db = this.getWritableDatabase();
}

/**
* Guestimate
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {

String crt_log_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_LOG + "(" +
COl_LOG_HUNTID + " INTEGER PRIMARY KEY, " +
COL_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP, " +
COL_LOG_BIRDNAME + " TEXT, " +
COL_LOG_SEENORSHOT + " TEXT, " +
COL_LOG_BIRDAGE + " REAL," +
COL_LOG_NUMSEENORSHOT + " INTEGER, " +
COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT + " TEXT," +
COL_LOG_ANIMALSHORORTYPECLAYSHOOT + " TEXT " +
")";
db.execSQL(crt_log_tbl_sql);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

/**
* Added so I could see the rows in the log
*/
public void dumpLogTable() {
Cursor csr = db.query(TABLE_LOG,null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr.close();
}

/**
* Guestimate. (note converts dates to YYYY-MM-DD format as stored )
* @param strDate
* @return
*/
public int getHuntIDforPastHunts(String strDate) {
int rv = 0;
String datevalues = strDate.split("/");
String srchdate = datevalues[2] + "-" + datevalues[1] + "-" + datevalues[0];
String whereclause = "date(" + COL_TIMESTAMP + ")=?";
String whereargs = new String{srchdate};
Cursor csr = db.query(TABLE_LOG,null,whereclause,whereargs,null,null,null);
if (csr.moveToFirst()) {
rv = csr.getInt(csr.getColumnIndex(COl_LOG_HUNTID));
}
return rv;
}

/**
* ALlows some testing data to be added
* @param birdname
* @param seenorshot
* @param birdage
* @param numseenorshot
* @param otheranimalshotorclayshoot
* @param animalshotortypeclayshoot
* @return
*/
public long insertLog(String birdname, String seenorshot,double birdage, int numseenorshot, String otheranimalshotorclayshoot, String animalshotortypeclayshoot) {
ContentValues cv = new ContentValues();
cv.put(COL_LOG_BIRDNAME,birdname);
cv.put(COL_LOG_SEENORSHOT,seenorshot);
cv.put(COL_LOG_BIRDAGE,birdage);
cv.put(COL_LOG_NUMSEENORSHOT,numseenorshot);
cv.put(COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT,otheranimalshotorclayshoot);
cv.put(COL_LOG_ANIMALSHORORTYPECLAYSHOOT,animalshotortypeclayshoot);
return db.insert(TABLE_LOG,null,cv);
}

/**
* Very much as was
* @param huntID
* @return
*/
public Cursor getPastHuntLogs(int huntID)
{
String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
"AnimalShotNameORTypeClaysShot FROM Log " +
"WHERE HuntID = " + String.valueOf(huntID) + //<<<<<<<<<< Changed
""; //<<<<<<<<<< ADDED so that WHERE...... can be commented out so select all
return db.rawQuery(query, null); //<<<<<<<< returns cursor directly
}
}



  • refer to the comments


PreviousHunts.java



public class PreviousHunts extends AppCompatActivity implements View.OnClickListener {

public TextView txtDate;
public String strDate = "";
public ListView lstView;
SimpleCursorAdapter adapter; //<<<<<<<<<< ADDED
DatabaseHelper dbHelp; //<<<<<<<<<< ADDED
Cursor cursor; //<<<<<<<<<< ADDED

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

Button btnDateGet = findViewById(R.id.btnGetHunts);
btnDateGet.setOnClickListener(this);

Button btnHome = findViewById(R.id.btnHuntsHome);
btnHome.setOnClickListener(this);

txtDate = findViewById(R.id.txtDateThing);
lstView = findViewById(R.id.lstView);
dbHelp = new DatabaseHelper(this); //<<<<<<<<<< ADDED
addSomeTestData(); //<<<<<<<<<< ADDDED

}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnGetHunts:
getDate();
break;
case R.id.btnHuntsHome:
goHome();
break;
}
}

private void getDate()
{
final Calendar c = Calendar.getInstance();
final int Year = c.get(Calendar.YEAR);
final int Month = c.get(Calendar.MONTH);
final int Day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
String day = String.valueOf(dayOfMonth);
String Smonth = String.valueOf(month + 1);
String sYear = String.valueOf(year);

txtDate.setText(day + "/" + Smonth + "/" + sYear);
strDate = day + "/" + Smonth + "/" + sYear;
displayHuntList(strDate); //<<<<<<<<<< CHANGED calls displayHuntList
}
},Year, Month, Day);
datePickerDialog.show();
}


//<<<<<<<<<< NEW replaces last_hunt code

public void displayHuntList(String strDate) {
int huntID = dbHelp.getHuntIDforPastHunts(strDate);
if (huntID < 1) {
Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
}
cursor = dbHelp.getPastHuntLogs(huntID);
//<<<<<<<<<< Only create a SimpleCursorAdapter instance once and reuse
//<<<<<<<<<< NOTE!! Stock layout used for convenience >>>>>>>>>>
if (adapter == null) {
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cursor,
new String{DatabaseHelper.COL_LOG_BIRDNAME,
DatabaseHelper.COL_LOG_SEENORSHOT},
new int{android.R.id.text1,android.R.id.text2},
0
);
lstView.setAdapter(adapter);
} else {
// if called after the initial call just swap the cursor to refresh the list
adapter.swapCursor(cursor);
}
}

private void goHome()
{
//<<<<<<<<<< Commented body out so hitting HOME does nothing >>>>>>>>>>
/*
Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
startActivity(intent);
finish();
*/
}

/**
* Added so some testing data is added (note 2 rows added each time App is run)
* All rows have a timestamp as per the day the App is run (ok for testing)
*/
private void addSomeTestData() {
dbHelp.insertLog("Pheasant","Seen",1.75,100,"N/A","N/A");
dbHelp.insertLog("Grouse","SHOT",2.3,10,"NO","MAYBE");
dbHelp.dumpLogTable();
}
}



  • Hopefully much of the code will be familiar, comments should help

  • Note the list only ever shows 1 as selection is via the huntID (perhaps this is from another table, I guess so) (commenting out the WHERE line in the getPastHuntLogs method in DatabaseHelper.java will list all rows).


Result 1



When started (note I set the background of the ListView so it was easy to see):-



enter image description here



Result 2



After clicking Button and selecting current date :-



enter image description here






share|improve this answer































    0














    Actually went a less complicated but easier route after this. Simply made a long string which actually helped with later parts of the code in the long run. Thanks all for the help.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443608%2fsqlite-database-in-listview-android%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Try adding database code after date selection.



      Like this



      private void getDate() {
      final Calendar c = Calendar.getInstance();
      final int Year = c.get(Calendar.YEAR);
      final int Month = c.get(Calendar.MONTH);
      final int Day = c.get(Calendar.DAY_OF_MONTH);

      DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
      {
      @Override
      public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
      {
      String day = String.valueOf(dayOfMonth);
      String Smonth = String.valueOf(month + 1);
      String sYear = String.valueOf(year);

      txtDate.setText(day + "/" + Smonth + "/" + sYear);
      strDate = day + "/" + Smonth + "/" + sYear;

      // Try database code here
      DatabaseHelper dbHelp = new DatabaseHelper(this);
      int huntID = dbHelp.getHuntIDforPastHunts(strDate);

      if (huntID == 0)
      {
      Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
      }
      else
      {
      LastPart(huntID);
      }

      }
      },Year, Month, Day);
      datePickerDialog.show();
      }





      share|improve this answer
























      • It didn't work unfortunately, no change in any display. Thanks for trying though.

        – Alexander Mcmichael
        Nov 23 '18 at 9:32











      • @AlexanderMcmichael can you make sure that query is correct? Or are there any values coming from your DB?

        – ahuja007
        Nov 23 '18 at 10:28


















      0














      Try adding database code after date selection.



      Like this



      private void getDate() {
      final Calendar c = Calendar.getInstance();
      final int Year = c.get(Calendar.YEAR);
      final int Month = c.get(Calendar.MONTH);
      final int Day = c.get(Calendar.DAY_OF_MONTH);

      DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
      {
      @Override
      public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
      {
      String day = String.valueOf(dayOfMonth);
      String Smonth = String.valueOf(month + 1);
      String sYear = String.valueOf(year);

      txtDate.setText(day + "/" + Smonth + "/" + sYear);
      strDate = day + "/" + Smonth + "/" + sYear;

      // Try database code here
      DatabaseHelper dbHelp = new DatabaseHelper(this);
      int huntID = dbHelp.getHuntIDforPastHunts(strDate);

      if (huntID == 0)
      {
      Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
      }
      else
      {
      LastPart(huntID);
      }

      }
      },Year, Month, Day);
      datePickerDialog.show();
      }





      share|improve this answer
























      • It didn't work unfortunately, no change in any display. Thanks for trying though.

        – Alexander Mcmichael
        Nov 23 '18 at 9:32











      • @AlexanderMcmichael can you make sure that query is correct? Or are there any values coming from your DB?

        – ahuja007
        Nov 23 '18 at 10:28
















      0












      0








      0







      Try adding database code after date selection.



      Like this



      private void getDate() {
      final Calendar c = Calendar.getInstance();
      final int Year = c.get(Calendar.YEAR);
      final int Month = c.get(Calendar.MONTH);
      final int Day = c.get(Calendar.DAY_OF_MONTH);

      DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
      {
      @Override
      public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
      {
      String day = String.valueOf(dayOfMonth);
      String Smonth = String.valueOf(month + 1);
      String sYear = String.valueOf(year);

      txtDate.setText(day + "/" + Smonth + "/" + sYear);
      strDate = day + "/" + Smonth + "/" + sYear;

      // Try database code here
      DatabaseHelper dbHelp = new DatabaseHelper(this);
      int huntID = dbHelp.getHuntIDforPastHunts(strDate);

      if (huntID == 0)
      {
      Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
      }
      else
      {
      LastPart(huntID);
      }

      }
      },Year, Month, Day);
      datePickerDialog.show();
      }





      share|improve this answer













      Try adding database code after date selection.



      Like this



      private void getDate() {
      final Calendar c = Calendar.getInstance();
      final int Year = c.get(Calendar.YEAR);
      final int Month = c.get(Calendar.MONTH);
      final int Day = c.get(Calendar.DAY_OF_MONTH);

      DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
      {
      @Override
      public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
      {
      String day = String.valueOf(dayOfMonth);
      String Smonth = String.valueOf(month + 1);
      String sYear = String.valueOf(year);

      txtDate.setText(day + "/" + Smonth + "/" + sYear);
      strDate = day + "/" + Smonth + "/" + sYear;

      // Try database code here
      DatabaseHelper dbHelp = new DatabaseHelper(this);
      int huntID = dbHelp.getHuntIDforPastHunts(strDate);

      if (huntID == 0)
      {
      Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
      }
      else
      {
      LastPart(huntID);
      }

      }
      },Year, Month, Day);
      datePickerDialog.show();
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 23 '18 at 9:26









      ahuja007ahuja007

      211110




      211110













      • It didn't work unfortunately, no change in any display. Thanks for trying though.

        – Alexander Mcmichael
        Nov 23 '18 at 9:32











      • @AlexanderMcmichael can you make sure that query is correct? Or are there any values coming from your DB?

        – ahuja007
        Nov 23 '18 at 10:28





















      • It didn't work unfortunately, no change in any display. Thanks for trying though.

        – Alexander Mcmichael
        Nov 23 '18 at 9:32











      • @AlexanderMcmichael can you make sure that query is correct? Or are there any values coming from your DB?

        – ahuja007
        Nov 23 '18 at 10:28



















      It didn't work unfortunately, no change in any display. Thanks for trying though.

      – Alexander Mcmichael
      Nov 23 '18 at 9:32





      It didn't work unfortunately, no change in any display. Thanks for trying though.

      – Alexander Mcmichael
      Nov 23 '18 at 9:32













      @AlexanderMcmichael can you make sure that query is correct? Or are there any values coming from your DB?

      – ahuja007
      Nov 23 '18 at 10:28







      @AlexanderMcmichael can you make sure that query is correct? Or are there any values coming from your DB?

      – ahuja007
      Nov 23 '18 at 10:28















      0














      The main issue you have is that you aren't invoking the population of the list after you have retrieved the date. Rather that the list view is being populated before the date has been retrieved.



      That is the code :-



          DatabaseHelper dbHelp = new DatabaseHelper(this);
      int huntID = dbHelp.getHuntIDforPastHunts(strDate);

      if (huntID == 0)
      {
      Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
      }
      else
      {
      LastPart(huntID);
      }


      will execute before you have got the date, so no date no data extracted.



      There are other issues, such as each time you hit the button you create a new adapter, when you should use one and refresh the list.



      The following is how I'd suggest doing it and it's working code and has been tested. However, for the testing I've had to guess much of the DatabaseHelper as all that was available was the getPastHuntLogs method.



      DatabaseHelper.java used was :-



      public class DatabaseHelper extends SQLiteOpenHelper {

      /**
      * Suggest using 1 definitaion of table and column names and to ALWAYS use the constants
      */
      public static final String DBNAME = "hunter";
      public static final int DBVERSION = 1;
      public static final String TABLE_LOG = "Log";
      public static final String COL_TIMESTAMP = "timestamp";
      public static final String COl_LOG_HUNTID = "HuntID";
      public static final String COL_LOG_BIRDNAME = "BirdName";
      public static final String COL_LOG_SEENORSHOT = "SeenOrShot";
      public static final String COL_LOG_BIRDAGE = "BirdAge";
      public static final String COL_LOG_NUMSEENORSHOT = "NumSeenOrShot";
      public static final String COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT = "OtherAnimalShotORClayShoot";
      public static final String COL_LOG_ANIMALSHORORTYPECLAYSHOOT = "AnimalShotNameORTypeClaysShot";

      SQLiteDatabase db;
      public DatabaseHelper(Context context) {
      super(context, DBNAME, null, DBVERSION);
      db = this.getWritableDatabase();
      }

      /**
      * Guestimate
      * @param db
      */
      @Override
      public void onCreate(SQLiteDatabase db) {

      String crt_log_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_LOG + "(" +
      COl_LOG_HUNTID + " INTEGER PRIMARY KEY, " +
      COL_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP, " +
      COL_LOG_BIRDNAME + " TEXT, " +
      COL_LOG_SEENORSHOT + " TEXT, " +
      COL_LOG_BIRDAGE + " REAL," +
      COL_LOG_NUMSEENORSHOT + " INTEGER, " +
      COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT + " TEXT," +
      COL_LOG_ANIMALSHORORTYPECLAYSHOOT + " TEXT " +
      ")";
      db.execSQL(crt_log_tbl_sql);
      }

      @Override
      public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

      }

      /**
      * Added so I could see the rows in the log
      */
      public void dumpLogTable() {
      Cursor csr = db.query(TABLE_LOG,null,null,null,null,null,null);
      DatabaseUtils.dumpCursor(csr);
      csr.close();
      }

      /**
      * Guestimate. (note converts dates to YYYY-MM-DD format as stored )
      * @param strDate
      * @return
      */
      public int getHuntIDforPastHunts(String strDate) {
      int rv = 0;
      String datevalues = strDate.split("/");
      String srchdate = datevalues[2] + "-" + datevalues[1] + "-" + datevalues[0];
      String whereclause = "date(" + COL_TIMESTAMP + ")=?";
      String whereargs = new String{srchdate};
      Cursor csr = db.query(TABLE_LOG,null,whereclause,whereargs,null,null,null);
      if (csr.moveToFirst()) {
      rv = csr.getInt(csr.getColumnIndex(COl_LOG_HUNTID));
      }
      return rv;
      }

      /**
      * ALlows some testing data to be added
      * @param birdname
      * @param seenorshot
      * @param birdage
      * @param numseenorshot
      * @param otheranimalshotorclayshoot
      * @param animalshotortypeclayshoot
      * @return
      */
      public long insertLog(String birdname, String seenorshot,double birdage, int numseenorshot, String otheranimalshotorclayshoot, String animalshotortypeclayshoot) {
      ContentValues cv = new ContentValues();
      cv.put(COL_LOG_BIRDNAME,birdname);
      cv.put(COL_LOG_SEENORSHOT,seenorshot);
      cv.put(COL_LOG_BIRDAGE,birdage);
      cv.put(COL_LOG_NUMSEENORSHOT,numseenorshot);
      cv.put(COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT,otheranimalshotorclayshoot);
      cv.put(COL_LOG_ANIMALSHORORTYPECLAYSHOOT,animalshotortypeclayshoot);
      return db.insert(TABLE_LOG,null,cv);
      }

      /**
      * Very much as was
      * @param huntID
      * @return
      */
      public Cursor getPastHuntLogs(int huntID)
      {
      String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
      "AnimalShotNameORTypeClaysShot FROM Log " +
      "WHERE HuntID = " + String.valueOf(huntID) + //<<<<<<<<<< Changed
      ""; //<<<<<<<<<< ADDED so that WHERE...... can be commented out so select all
      return db.rawQuery(query, null); //<<<<<<<< returns cursor directly
      }
      }



      • refer to the comments


      PreviousHunts.java



      public class PreviousHunts extends AppCompatActivity implements View.OnClickListener {

      public TextView txtDate;
      public String strDate = "";
      public ListView lstView;
      SimpleCursorAdapter adapter; //<<<<<<<<<< ADDED
      DatabaseHelper dbHelp; //<<<<<<<<<< ADDED
      Cursor cursor; //<<<<<<<<<< ADDED

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

      Button btnDateGet = findViewById(R.id.btnGetHunts);
      btnDateGet.setOnClickListener(this);

      Button btnHome = findViewById(R.id.btnHuntsHome);
      btnHome.setOnClickListener(this);

      txtDate = findViewById(R.id.txtDateThing);
      lstView = findViewById(R.id.lstView);
      dbHelp = new DatabaseHelper(this); //<<<<<<<<<< ADDED
      addSomeTestData(); //<<<<<<<<<< ADDDED

      }

      @Override
      public void onClick(View v)
      {
      switch (v.getId())
      {
      case R.id.btnGetHunts:
      getDate();
      break;
      case R.id.btnHuntsHome:
      goHome();
      break;
      }
      }

      private void getDate()
      {
      final Calendar c = Calendar.getInstance();
      final int Year = c.get(Calendar.YEAR);
      final int Month = c.get(Calendar.MONTH);
      final int Day = c.get(Calendar.DAY_OF_MONTH);

      DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
      {
      @Override
      public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
      {
      String day = String.valueOf(dayOfMonth);
      String Smonth = String.valueOf(month + 1);
      String sYear = String.valueOf(year);

      txtDate.setText(day + "/" + Smonth + "/" + sYear);
      strDate = day + "/" + Smonth + "/" + sYear;
      displayHuntList(strDate); //<<<<<<<<<< CHANGED calls displayHuntList
      }
      },Year, Month, Day);
      datePickerDialog.show();
      }


      //<<<<<<<<<< NEW replaces last_hunt code

      public void displayHuntList(String strDate) {
      int huntID = dbHelp.getHuntIDforPastHunts(strDate);
      if (huntID < 1) {
      Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
      }
      cursor = dbHelp.getPastHuntLogs(huntID);
      //<<<<<<<<<< Only create a SimpleCursorAdapter instance once and reuse
      //<<<<<<<<<< NOTE!! Stock layout used for convenience >>>>>>>>>>
      if (adapter == null) {
      adapter = new SimpleCursorAdapter(this,
      android.R.layout.simple_list_item_2,
      cursor,
      new String{DatabaseHelper.COL_LOG_BIRDNAME,
      DatabaseHelper.COL_LOG_SEENORSHOT},
      new int{android.R.id.text1,android.R.id.text2},
      0
      );
      lstView.setAdapter(adapter);
      } else {
      // if called after the initial call just swap the cursor to refresh the list
      adapter.swapCursor(cursor);
      }
      }

      private void goHome()
      {
      //<<<<<<<<<< Commented body out so hitting HOME does nothing >>>>>>>>>>
      /*
      Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
      startActivity(intent);
      finish();
      */
      }

      /**
      * Added so some testing data is added (note 2 rows added each time App is run)
      * All rows have a timestamp as per the day the App is run (ok for testing)
      */
      private void addSomeTestData() {
      dbHelp.insertLog("Pheasant","Seen",1.75,100,"N/A","N/A");
      dbHelp.insertLog("Grouse","SHOT",2.3,10,"NO","MAYBE");
      dbHelp.dumpLogTable();
      }
      }



      • Hopefully much of the code will be familiar, comments should help

      • Note the list only ever shows 1 as selection is via the huntID (perhaps this is from another table, I guess so) (commenting out the WHERE line in the getPastHuntLogs method in DatabaseHelper.java will list all rows).


      Result 1



      When started (note I set the background of the ListView so it was easy to see):-



      enter image description here



      Result 2



      After clicking Button and selecting current date :-



      enter image description here






      share|improve this answer




























        0














        The main issue you have is that you aren't invoking the population of the list after you have retrieved the date. Rather that the list view is being populated before the date has been retrieved.



        That is the code :-



            DatabaseHelper dbHelp = new DatabaseHelper(this);
        int huntID = dbHelp.getHuntIDforPastHunts(strDate);

        if (huntID == 0)
        {
        Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
        }
        else
        {
        LastPart(huntID);
        }


        will execute before you have got the date, so no date no data extracted.



        There are other issues, such as each time you hit the button you create a new adapter, when you should use one and refresh the list.



        The following is how I'd suggest doing it and it's working code and has been tested. However, for the testing I've had to guess much of the DatabaseHelper as all that was available was the getPastHuntLogs method.



        DatabaseHelper.java used was :-



        public class DatabaseHelper extends SQLiteOpenHelper {

        /**
        * Suggest using 1 definitaion of table and column names and to ALWAYS use the constants
        */
        public static final String DBNAME = "hunter";
        public static final int DBVERSION = 1;
        public static final String TABLE_LOG = "Log";
        public static final String COL_TIMESTAMP = "timestamp";
        public static final String COl_LOG_HUNTID = "HuntID";
        public static final String COL_LOG_BIRDNAME = "BirdName";
        public static final String COL_LOG_SEENORSHOT = "SeenOrShot";
        public static final String COL_LOG_BIRDAGE = "BirdAge";
        public static final String COL_LOG_NUMSEENORSHOT = "NumSeenOrShot";
        public static final String COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT = "OtherAnimalShotORClayShoot";
        public static final String COL_LOG_ANIMALSHORORTYPECLAYSHOOT = "AnimalShotNameORTypeClaysShot";

        SQLiteDatabase db;
        public DatabaseHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        db = this.getWritableDatabase();
        }

        /**
        * Guestimate
        * @param db
        */
        @Override
        public void onCreate(SQLiteDatabase db) {

        String crt_log_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_LOG + "(" +
        COl_LOG_HUNTID + " INTEGER PRIMARY KEY, " +
        COL_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP, " +
        COL_LOG_BIRDNAME + " TEXT, " +
        COL_LOG_SEENORSHOT + " TEXT, " +
        COL_LOG_BIRDAGE + " REAL," +
        COL_LOG_NUMSEENORSHOT + " INTEGER, " +
        COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT + " TEXT," +
        COL_LOG_ANIMALSHORORTYPECLAYSHOOT + " TEXT " +
        ")";
        db.execSQL(crt_log_tbl_sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }

        /**
        * Added so I could see the rows in the log
        */
        public void dumpLogTable() {
        Cursor csr = db.query(TABLE_LOG,null,null,null,null,null,null);
        DatabaseUtils.dumpCursor(csr);
        csr.close();
        }

        /**
        * Guestimate. (note converts dates to YYYY-MM-DD format as stored )
        * @param strDate
        * @return
        */
        public int getHuntIDforPastHunts(String strDate) {
        int rv = 0;
        String datevalues = strDate.split("/");
        String srchdate = datevalues[2] + "-" + datevalues[1] + "-" + datevalues[0];
        String whereclause = "date(" + COL_TIMESTAMP + ")=?";
        String whereargs = new String{srchdate};
        Cursor csr = db.query(TABLE_LOG,null,whereclause,whereargs,null,null,null);
        if (csr.moveToFirst()) {
        rv = csr.getInt(csr.getColumnIndex(COl_LOG_HUNTID));
        }
        return rv;
        }

        /**
        * ALlows some testing data to be added
        * @param birdname
        * @param seenorshot
        * @param birdage
        * @param numseenorshot
        * @param otheranimalshotorclayshoot
        * @param animalshotortypeclayshoot
        * @return
        */
        public long insertLog(String birdname, String seenorshot,double birdage, int numseenorshot, String otheranimalshotorclayshoot, String animalshotortypeclayshoot) {
        ContentValues cv = new ContentValues();
        cv.put(COL_LOG_BIRDNAME,birdname);
        cv.put(COL_LOG_SEENORSHOT,seenorshot);
        cv.put(COL_LOG_BIRDAGE,birdage);
        cv.put(COL_LOG_NUMSEENORSHOT,numseenorshot);
        cv.put(COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT,otheranimalshotorclayshoot);
        cv.put(COL_LOG_ANIMALSHORORTYPECLAYSHOOT,animalshotortypeclayshoot);
        return db.insert(TABLE_LOG,null,cv);
        }

        /**
        * Very much as was
        * @param huntID
        * @return
        */
        public Cursor getPastHuntLogs(int huntID)
        {
        String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
        "AnimalShotNameORTypeClaysShot FROM Log " +
        "WHERE HuntID = " + String.valueOf(huntID) + //<<<<<<<<<< Changed
        ""; //<<<<<<<<<< ADDED so that WHERE...... can be commented out so select all
        return db.rawQuery(query, null); //<<<<<<<< returns cursor directly
        }
        }



        • refer to the comments


        PreviousHunts.java



        public class PreviousHunts extends AppCompatActivity implements View.OnClickListener {

        public TextView txtDate;
        public String strDate = "";
        public ListView lstView;
        SimpleCursorAdapter adapter; //<<<<<<<<<< ADDED
        DatabaseHelper dbHelp; //<<<<<<<<<< ADDED
        Cursor cursor; //<<<<<<<<<< ADDED

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

        Button btnDateGet = findViewById(R.id.btnGetHunts);
        btnDateGet.setOnClickListener(this);

        Button btnHome = findViewById(R.id.btnHuntsHome);
        btnHome.setOnClickListener(this);

        txtDate = findViewById(R.id.txtDateThing);
        lstView = findViewById(R.id.lstView);
        dbHelp = new DatabaseHelper(this); //<<<<<<<<<< ADDED
        addSomeTestData(); //<<<<<<<<<< ADDDED

        }

        @Override
        public void onClick(View v)
        {
        switch (v.getId())
        {
        case R.id.btnGetHunts:
        getDate();
        break;
        case R.id.btnHuntsHome:
        goHome();
        break;
        }
        }

        private void getDate()
        {
        final Calendar c = Calendar.getInstance();
        final int Year = c.get(Calendar.YEAR);
        final int Month = c.get(Calendar.MONTH);
        final int Day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
        {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
        {
        String day = String.valueOf(dayOfMonth);
        String Smonth = String.valueOf(month + 1);
        String sYear = String.valueOf(year);

        txtDate.setText(day + "/" + Smonth + "/" + sYear);
        strDate = day + "/" + Smonth + "/" + sYear;
        displayHuntList(strDate); //<<<<<<<<<< CHANGED calls displayHuntList
        }
        },Year, Month, Day);
        datePickerDialog.show();
        }


        //<<<<<<<<<< NEW replaces last_hunt code

        public void displayHuntList(String strDate) {
        int huntID = dbHelp.getHuntIDforPastHunts(strDate);
        if (huntID < 1) {
        Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
        }
        cursor = dbHelp.getPastHuntLogs(huntID);
        //<<<<<<<<<< Only create a SimpleCursorAdapter instance once and reuse
        //<<<<<<<<<< NOTE!! Stock layout used for convenience >>>>>>>>>>
        if (adapter == null) {
        adapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_2,
        cursor,
        new String{DatabaseHelper.COL_LOG_BIRDNAME,
        DatabaseHelper.COL_LOG_SEENORSHOT},
        new int{android.R.id.text1,android.R.id.text2},
        0
        );
        lstView.setAdapter(adapter);
        } else {
        // if called after the initial call just swap the cursor to refresh the list
        adapter.swapCursor(cursor);
        }
        }

        private void goHome()
        {
        //<<<<<<<<<< Commented body out so hitting HOME does nothing >>>>>>>>>>
        /*
        Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
        startActivity(intent);
        finish();
        */
        }

        /**
        * Added so some testing data is added (note 2 rows added each time App is run)
        * All rows have a timestamp as per the day the App is run (ok for testing)
        */
        private void addSomeTestData() {
        dbHelp.insertLog("Pheasant","Seen",1.75,100,"N/A","N/A");
        dbHelp.insertLog("Grouse","SHOT",2.3,10,"NO","MAYBE");
        dbHelp.dumpLogTable();
        }
        }



        • Hopefully much of the code will be familiar, comments should help

        • Note the list only ever shows 1 as selection is via the huntID (perhaps this is from another table, I guess so) (commenting out the WHERE line in the getPastHuntLogs method in DatabaseHelper.java will list all rows).


        Result 1



        When started (note I set the background of the ListView so it was easy to see):-



        enter image description here



        Result 2



        After clicking Button and selecting current date :-



        enter image description here






        share|improve this answer


























          0












          0








          0







          The main issue you have is that you aren't invoking the population of the list after you have retrieved the date. Rather that the list view is being populated before the date has been retrieved.



          That is the code :-



              DatabaseHelper dbHelp = new DatabaseHelper(this);
          int huntID = dbHelp.getHuntIDforPastHunts(strDate);

          if (huntID == 0)
          {
          Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
          }
          else
          {
          LastPart(huntID);
          }


          will execute before you have got the date, so no date no data extracted.



          There are other issues, such as each time you hit the button you create a new adapter, when you should use one and refresh the list.



          The following is how I'd suggest doing it and it's working code and has been tested. However, for the testing I've had to guess much of the DatabaseHelper as all that was available was the getPastHuntLogs method.



          DatabaseHelper.java used was :-



          public class DatabaseHelper extends SQLiteOpenHelper {

          /**
          * Suggest using 1 definitaion of table and column names and to ALWAYS use the constants
          */
          public static final String DBNAME = "hunter";
          public static final int DBVERSION = 1;
          public static final String TABLE_LOG = "Log";
          public static final String COL_TIMESTAMP = "timestamp";
          public static final String COl_LOG_HUNTID = "HuntID";
          public static final String COL_LOG_BIRDNAME = "BirdName";
          public static final String COL_LOG_SEENORSHOT = "SeenOrShot";
          public static final String COL_LOG_BIRDAGE = "BirdAge";
          public static final String COL_LOG_NUMSEENORSHOT = "NumSeenOrShot";
          public static final String COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT = "OtherAnimalShotORClayShoot";
          public static final String COL_LOG_ANIMALSHORORTYPECLAYSHOOT = "AnimalShotNameORTypeClaysShot";

          SQLiteDatabase db;
          public DatabaseHelper(Context context) {
          super(context, DBNAME, null, DBVERSION);
          db = this.getWritableDatabase();
          }

          /**
          * Guestimate
          * @param db
          */
          @Override
          public void onCreate(SQLiteDatabase db) {

          String crt_log_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_LOG + "(" +
          COl_LOG_HUNTID + " INTEGER PRIMARY KEY, " +
          COL_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP, " +
          COL_LOG_BIRDNAME + " TEXT, " +
          COL_LOG_SEENORSHOT + " TEXT, " +
          COL_LOG_BIRDAGE + " REAL," +
          COL_LOG_NUMSEENORSHOT + " INTEGER, " +
          COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT + " TEXT," +
          COL_LOG_ANIMALSHORORTYPECLAYSHOOT + " TEXT " +
          ")";
          db.execSQL(crt_log_tbl_sql);
          }

          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

          }

          /**
          * Added so I could see the rows in the log
          */
          public void dumpLogTable() {
          Cursor csr = db.query(TABLE_LOG,null,null,null,null,null,null);
          DatabaseUtils.dumpCursor(csr);
          csr.close();
          }

          /**
          * Guestimate. (note converts dates to YYYY-MM-DD format as stored )
          * @param strDate
          * @return
          */
          public int getHuntIDforPastHunts(String strDate) {
          int rv = 0;
          String datevalues = strDate.split("/");
          String srchdate = datevalues[2] + "-" + datevalues[1] + "-" + datevalues[0];
          String whereclause = "date(" + COL_TIMESTAMP + ")=?";
          String whereargs = new String{srchdate};
          Cursor csr = db.query(TABLE_LOG,null,whereclause,whereargs,null,null,null);
          if (csr.moveToFirst()) {
          rv = csr.getInt(csr.getColumnIndex(COl_LOG_HUNTID));
          }
          return rv;
          }

          /**
          * ALlows some testing data to be added
          * @param birdname
          * @param seenorshot
          * @param birdage
          * @param numseenorshot
          * @param otheranimalshotorclayshoot
          * @param animalshotortypeclayshoot
          * @return
          */
          public long insertLog(String birdname, String seenorshot,double birdage, int numseenorshot, String otheranimalshotorclayshoot, String animalshotortypeclayshoot) {
          ContentValues cv = new ContentValues();
          cv.put(COL_LOG_BIRDNAME,birdname);
          cv.put(COL_LOG_SEENORSHOT,seenorshot);
          cv.put(COL_LOG_BIRDAGE,birdage);
          cv.put(COL_LOG_NUMSEENORSHOT,numseenorshot);
          cv.put(COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT,otheranimalshotorclayshoot);
          cv.put(COL_LOG_ANIMALSHORORTYPECLAYSHOOT,animalshotortypeclayshoot);
          return db.insert(TABLE_LOG,null,cv);
          }

          /**
          * Very much as was
          * @param huntID
          * @return
          */
          public Cursor getPastHuntLogs(int huntID)
          {
          String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
          "AnimalShotNameORTypeClaysShot FROM Log " +
          "WHERE HuntID = " + String.valueOf(huntID) + //<<<<<<<<<< Changed
          ""; //<<<<<<<<<< ADDED so that WHERE...... can be commented out so select all
          return db.rawQuery(query, null); //<<<<<<<< returns cursor directly
          }
          }



          • refer to the comments


          PreviousHunts.java



          public class PreviousHunts extends AppCompatActivity implements View.OnClickListener {

          public TextView txtDate;
          public String strDate = "";
          public ListView lstView;
          SimpleCursorAdapter adapter; //<<<<<<<<<< ADDED
          DatabaseHelper dbHelp; //<<<<<<<<<< ADDED
          Cursor cursor; //<<<<<<<<<< ADDED

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

          Button btnDateGet = findViewById(R.id.btnGetHunts);
          btnDateGet.setOnClickListener(this);

          Button btnHome = findViewById(R.id.btnHuntsHome);
          btnHome.setOnClickListener(this);

          txtDate = findViewById(R.id.txtDateThing);
          lstView = findViewById(R.id.lstView);
          dbHelp = new DatabaseHelper(this); //<<<<<<<<<< ADDED
          addSomeTestData(); //<<<<<<<<<< ADDDED

          }

          @Override
          public void onClick(View v)
          {
          switch (v.getId())
          {
          case R.id.btnGetHunts:
          getDate();
          break;
          case R.id.btnHuntsHome:
          goHome();
          break;
          }
          }

          private void getDate()
          {
          final Calendar c = Calendar.getInstance();
          final int Year = c.get(Calendar.YEAR);
          final int Month = c.get(Calendar.MONTH);
          final int Day = c.get(Calendar.DAY_OF_MONTH);

          DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
          {
          @Override
          public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
          {
          String day = String.valueOf(dayOfMonth);
          String Smonth = String.valueOf(month + 1);
          String sYear = String.valueOf(year);

          txtDate.setText(day + "/" + Smonth + "/" + sYear);
          strDate = day + "/" + Smonth + "/" + sYear;
          displayHuntList(strDate); //<<<<<<<<<< CHANGED calls displayHuntList
          }
          },Year, Month, Day);
          datePickerDialog.show();
          }


          //<<<<<<<<<< NEW replaces last_hunt code

          public void displayHuntList(String strDate) {
          int huntID = dbHelp.getHuntIDforPastHunts(strDate);
          if (huntID < 1) {
          Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
          }
          cursor = dbHelp.getPastHuntLogs(huntID);
          //<<<<<<<<<< Only create a SimpleCursorAdapter instance once and reuse
          //<<<<<<<<<< NOTE!! Stock layout used for convenience >>>>>>>>>>
          if (adapter == null) {
          adapter = new SimpleCursorAdapter(this,
          android.R.layout.simple_list_item_2,
          cursor,
          new String{DatabaseHelper.COL_LOG_BIRDNAME,
          DatabaseHelper.COL_LOG_SEENORSHOT},
          new int{android.R.id.text1,android.R.id.text2},
          0
          );
          lstView.setAdapter(adapter);
          } else {
          // if called after the initial call just swap the cursor to refresh the list
          adapter.swapCursor(cursor);
          }
          }

          private void goHome()
          {
          //<<<<<<<<<< Commented body out so hitting HOME does nothing >>>>>>>>>>
          /*
          Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
          startActivity(intent);
          finish();
          */
          }

          /**
          * Added so some testing data is added (note 2 rows added each time App is run)
          * All rows have a timestamp as per the day the App is run (ok for testing)
          */
          private void addSomeTestData() {
          dbHelp.insertLog("Pheasant","Seen",1.75,100,"N/A","N/A");
          dbHelp.insertLog("Grouse","SHOT",2.3,10,"NO","MAYBE");
          dbHelp.dumpLogTable();
          }
          }



          • Hopefully much of the code will be familiar, comments should help

          • Note the list only ever shows 1 as selection is via the huntID (perhaps this is from another table, I guess so) (commenting out the WHERE line in the getPastHuntLogs method in DatabaseHelper.java will list all rows).


          Result 1



          When started (note I set the background of the ListView so it was easy to see):-



          enter image description here



          Result 2



          After clicking Button and selecting current date :-



          enter image description here






          share|improve this answer













          The main issue you have is that you aren't invoking the population of the list after you have retrieved the date. Rather that the list view is being populated before the date has been retrieved.



          That is the code :-



              DatabaseHelper dbHelp = new DatabaseHelper(this);
          int huntID = dbHelp.getHuntIDforPastHunts(strDate);

          if (huntID == 0)
          {
          Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
          }
          else
          {
          LastPart(huntID);
          }


          will execute before you have got the date, so no date no data extracted.



          There are other issues, such as each time you hit the button you create a new adapter, when you should use one and refresh the list.



          The following is how I'd suggest doing it and it's working code and has been tested. However, for the testing I've had to guess much of the DatabaseHelper as all that was available was the getPastHuntLogs method.



          DatabaseHelper.java used was :-



          public class DatabaseHelper extends SQLiteOpenHelper {

          /**
          * Suggest using 1 definitaion of table and column names and to ALWAYS use the constants
          */
          public static final String DBNAME = "hunter";
          public static final int DBVERSION = 1;
          public static final String TABLE_LOG = "Log";
          public static final String COL_TIMESTAMP = "timestamp";
          public static final String COl_LOG_HUNTID = "HuntID";
          public static final String COL_LOG_BIRDNAME = "BirdName";
          public static final String COL_LOG_SEENORSHOT = "SeenOrShot";
          public static final String COL_LOG_BIRDAGE = "BirdAge";
          public static final String COL_LOG_NUMSEENORSHOT = "NumSeenOrShot";
          public static final String COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT = "OtherAnimalShotORClayShoot";
          public static final String COL_LOG_ANIMALSHORORTYPECLAYSHOOT = "AnimalShotNameORTypeClaysShot";

          SQLiteDatabase db;
          public DatabaseHelper(Context context) {
          super(context, DBNAME, null, DBVERSION);
          db = this.getWritableDatabase();
          }

          /**
          * Guestimate
          * @param db
          */
          @Override
          public void onCreate(SQLiteDatabase db) {

          String crt_log_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_LOG + "(" +
          COl_LOG_HUNTID + " INTEGER PRIMARY KEY, " +
          COL_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP, " +
          COL_LOG_BIRDNAME + " TEXT, " +
          COL_LOG_SEENORSHOT + " TEXT, " +
          COL_LOG_BIRDAGE + " REAL," +
          COL_LOG_NUMSEENORSHOT + " INTEGER, " +
          COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT + " TEXT," +
          COL_LOG_ANIMALSHORORTYPECLAYSHOOT + " TEXT " +
          ")";
          db.execSQL(crt_log_tbl_sql);
          }

          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

          }

          /**
          * Added so I could see the rows in the log
          */
          public void dumpLogTable() {
          Cursor csr = db.query(TABLE_LOG,null,null,null,null,null,null);
          DatabaseUtils.dumpCursor(csr);
          csr.close();
          }

          /**
          * Guestimate. (note converts dates to YYYY-MM-DD format as stored )
          * @param strDate
          * @return
          */
          public int getHuntIDforPastHunts(String strDate) {
          int rv = 0;
          String datevalues = strDate.split("/");
          String srchdate = datevalues[2] + "-" + datevalues[1] + "-" + datevalues[0];
          String whereclause = "date(" + COL_TIMESTAMP + ")=?";
          String whereargs = new String{srchdate};
          Cursor csr = db.query(TABLE_LOG,null,whereclause,whereargs,null,null,null);
          if (csr.moveToFirst()) {
          rv = csr.getInt(csr.getColumnIndex(COl_LOG_HUNTID));
          }
          return rv;
          }

          /**
          * ALlows some testing data to be added
          * @param birdname
          * @param seenorshot
          * @param birdage
          * @param numseenorshot
          * @param otheranimalshotorclayshoot
          * @param animalshotortypeclayshoot
          * @return
          */
          public long insertLog(String birdname, String seenorshot,double birdage, int numseenorshot, String otheranimalshotorclayshoot, String animalshotortypeclayshoot) {
          ContentValues cv = new ContentValues();
          cv.put(COL_LOG_BIRDNAME,birdname);
          cv.put(COL_LOG_SEENORSHOT,seenorshot);
          cv.put(COL_LOG_BIRDAGE,birdage);
          cv.put(COL_LOG_NUMSEENORSHOT,numseenorshot);
          cv.put(COL_LOG_OTHERANIMALSSHOTORCLAYSHOOT,otheranimalshotorclayshoot);
          cv.put(COL_LOG_ANIMALSHORORTYPECLAYSHOOT,animalshotortypeclayshoot);
          return db.insert(TABLE_LOG,null,cv);
          }

          /**
          * Very much as was
          * @param huntID
          * @return
          */
          public Cursor getPastHuntLogs(int huntID)
          {
          String query = "SELECT HuntID AS _id, BirdName, SeenORShot, BirdAge, NumSeenORShot, OtherAnimalShotORClayShoot, " +
          "AnimalShotNameORTypeClaysShot FROM Log " +
          "WHERE HuntID = " + String.valueOf(huntID) + //<<<<<<<<<< Changed
          ""; //<<<<<<<<<< ADDED so that WHERE...... can be commented out so select all
          return db.rawQuery(query, null); //<<<<<<<< returns cursor directly
          }
          }



          • refer to the comments


          PreviousHunts.java



          public class PreviousHunts extends AppCompatActivity implements View.OnClickListener {

          public TextView txtDate;
          public String strDate = "";
          public ListView lstView;
          SimpleCursorAdapter adapter; //<<<<<<<<<< ADDED
          DatabaseHelper dbHelp; //<<<<<<<<<< ADDED
          Cursor cursor; //<<<<<<<<<< ADDED

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

          Button btnDateGet = findViewById(R.id.btnGetHunts);
          btnDateGet.setOnClickListener(this);

          Button btnHome = findViewById(R.id.btnHuntsHome);
          btnHome.setOnClickListener(this);

          txtDate = findViewById(R.id.txtDateThing);
          lstView = findViewById(R.id.lstView);
          dbHelp = new DatabaseHelper(this); //<<<<<<<<<< ADDED
          addSomeTestData(); //<<<<<<<<<< ADDDED

          }

          @Override
          public void onClick(View v)
          {
          switch (v.getId())
          {
          case R.id.btnGetHunts:
          getDate();
          break;
          case R.id.btnHuntsHome:
          goHome();
          break;
          }
          }

          private void getDate()
          {
          final Calendar c = Calendar.getInstance();
          final int Year = c.get(Calendar.YEAR);
          final int Month = c.get(Calendar.MONTH);
          final int Day = c.get(Calendar.DAY_OF_MONTH);

          DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
          {
          @Override
          public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
          {
          String day = String.valueOf(dayOfMonth);
          String Smonth = String.valueOf(month + 1);
          String sYear = String.valueOf(year);

          txtDate.setText(day + "/" + Smonth + "/" + sYear);
          strDate = day + "/" + Smonth + "/" + sYear;
          displayHuntList(strDate); //<<<<<<<<<< CHANGED calls displayHuntList
          }
          },Year, Month, Day);
          datePickerDialog.show();
          }


          //<<<<<<<<<< NEW replaces last_hunt code

          public void displayHuntList(String strDate) {
          int huntID = dbHelp.getHuntIDforPastHunts(strDate);
          if (huntID < 1) {
          Toast.makeText(getApplicationContext(), "No Hunts on that day.", Toast.LENGTH_LONG).show();
          }
          cursor = dbHelp.getPastHuntLogs(huntID);
          //<<<<<<<<<< Only create a SimpleCursorAdapter instance once and reuse
          //<<<<<<<<<< NOTE!! Stock layout used for convenience >>>>>>>>>>
          if (adapter == null) {
          adapter = new SimpleCursorAdapter(this,
          android.R.layout.simple_list_item_2,
          cursor,
          new String{DatabaseHelper.COL_LOG_BIRDNAME,
          DatabaseHelper.COL_LOG_SEENORSHOT},
          new int{android.R.id.text1,android.R.id.text2},
          0
          );
          lstView.setAdapter(adapter);
          } else {
          // if called after the initial call just swap the cursor to refresh the list
          adapter.swapCursor(cursor);
          }
          }

          private void goHome()
          {
          //<<<<<<<<<< Commented body out so hitting HOME does nothing >>>>>>>>>>
          /*
          Intent intent = new Intent(PreviousHunts.this, Home_Screen.class);
          startActivity(intent);
          finish();
          */
          }

          /**
          * Added so some testing data is added (note 2 rows added each time App is run)
          * All rows have a timestamp as per the day the App is run (ok for testing)
          */
          private void addSomeTestData() {
          dbHelp.insertLog("Pheasant","Seen",1.75,100,"N/A","N/A");
          dbHelp.insertLog("Grouse","SHOT",2.3,10,"NO","MAYBE");
          dbHelp.dumpLogTable();
          }
          }



          • Hopefully much of the code will be familiar, comments should help

          • Note the list only ever shows 1 as selection is via the huntID (perhaps this is from another table, I guess so) (commenting out the WHERE line in the getPastHuntLogs method in DatabaseHelper.java will list all rows).


          Result 1



          When started (note I set the background of the ListView so it was easy to see):-



          enter image description here



          Result 2



          After clicking Button and selecting current date :-



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 11:44









          MikeTMikeT

          18.2k112743




          18.2k112743























              0














              Actually went a less complicated but easier route after this. Simply made a long string which actually helped with later parts of the code in the long run. Thanks all for the help.






              share|improve this answer




























                0














                Actually went a less complicated but easier route after this. Simply made a long string which actually helped with later parts of the code in the long run. Thanks all for the help.






                share|improve this answer


























                  0












                  0








                  0







                  Actually went a less complicated but easier route after this. Simply made a long string which actually helped with later parts of the code in the long run. Thanks all for the help.






                  share|improve this answer













                  Actually went a less complicated but easier route after this. Simply made a long string which actually helped with later parts of the code in the long run. Thanks all for the help.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 20 at 15:55









                  Alexander McmichaelAlexander Mcmichael

                  267




                  267






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443608%2fsqlite-database-in-listview-android%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown