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.
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.