首页 > 解决方案 > ListView Android 中的 SQLite 数据库

问题描述

在 android studio 的列表视图中显示 SQLite 数据库中的一些记录时出现问题。显示列表视图的代码是:

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 是:

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

问题是什么都没有发生。运行日志中没有错误,也没有显示任何内容。帮助将不胜感激。

标签: androidsqlitesimplecursoradapter

解决方案


尝试在选择日期后添加数据库代码。

像这样

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();
}

推荐阅读