【问题标题】:Trouble Displaying ListView Elements from SQLite Table无法从 SQLite 表中显示 ListView 元素
【发布时间】:2021-01-30 12:24:47
【问题描述】:

我正在努力从 Android Studio 中的 SQLite 表中提取元素并在 ListView 中显示这些元素。我认为错误所在的区域是当我将 List 适配器设置为我的数组时。

首先,这是我的 SQHelper 类中的代码。它检索要显示的项目列表。

 public ArrayList<Course> getCourses()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<Course> courses = new ArrayList<>();
        Cursor cursor = null;


        cursor = db.query(TABLE_COURSE,null,null,null,null,null,null);
        if(cursor!=null)
            {
                cursor.moveToFirst();
                do {
                    int id = cursor.getInt(cursor.getColumnIndex(COLUMN_COURSE_ID));
                    String title = cursor.getString(cursor.getColumnIndex(COLUMN_COURSE_TITLE));
                    String code = cursor.getString(cursor.getColumnIndex(COLUMN_COURSE_CODE));

                    System.out.println("ID: "+id+"\nTITLE: "+title+"\nCODE"+code);

                    courses.add(new Course(id,title,code));
                    System.out.println("SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES");

                } while(cursor.moveToNext());
            }
        db.close();
 
        Log.d("SQHELPER","COURSES:");
        for(Course c : courses)
        {
            System.out.println(c.getCourseCode()+c.getCourseTitle());
        }
        return courses;
    }

请注意,该函数首先打印添加的每个元素,然后在返回之前输出整个课程列表。这对于日志的输出很重要。

第二个是 onClickListener,用于尝试向此列表添加元素。

saveButton.setOnClickListener(save_view ->
        {
            String title = courseTitle.getText().toString();
            String code = courseCode.getText().toString();

            SqHelper sqHelper = new SqHelper(getContext());
            sqHelper.insertCourse(new Course(-1,title,code));

            ((MainActivity)getActivity()).DisplayCourses();

            Toast.makeText(getContext(),"Saved!",Toast.LENGTH_SHORT).show();
            getDialog().dismiss();
        });

最后是上面调用的函数DisplayCourses()。

public void DisplayCourses()
{
    ArrayList<Course> courses = sqHelper.getCourses();

    courseAdapter = new ListViewAdapter(this, courses);
    listView = findViewById(R.id.course_list);
    listView.setAdapter(courseAdapter);
    ((ListViewAdapter) listView.getAdapter()).notifyDataSetChanged();
}

如前所述,SQHelper 函数会在添加每个元素时显示它,然后打印整个课程数组。这是我尝试插入新课程时的输出 (LOGCAT)。

2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: ID: 1
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: TITLE: Mini Capstone
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: CODECOEN 390
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: **ID: 2**
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: TITLE: fdsaf
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: CODEfdasfa
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: **ID: 3**
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: TITLE: fdasf
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: CODEasdfadsfa
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: **ID: 4**
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: TITLE: fasd
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: CODEfdjaksl;f
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: **ID: 5**
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: TITLE: TEST
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: CODETESTCODE
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: ID: 6
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: TITLE: TEST TITLE
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: CODETEST CODE
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: ID: 7
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: TITLE: why this no work
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: CODEwhyyyy
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 D/SQHELPER: COURSES:
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: **whyyyywhy this no work**
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/chatty: uid=10151(com.example.a40048841_ass3) **identical 5 lines**
2020-10-15 20:08:11.777 12875-12875/com.example.a40048841_ass3 I/System.out: **whyyyywhy this no work**

请注意,添加类时,它们的 ID、标题和代码看起来相同。但是当整个列表被打印出来时,它们都是一样的。这导致列表如下图所示。

总而言之,我试图在 SQLite 表的 listView 中显示元素列表。将元素插入要传递给我的适配器的数组时,以某种方式添加的课程属性不会加起来。

注意 ListView 适配器不是问题;我已经用硬编码的课程列表对其进行了测试,并以唯一的方式显示它们。

感谢您的宝贵时间!

编辑:这是课程课程

package com.example.a40048841_ass3;

import java.util.ArrayList;
import java.util.Random;

public class Course {
    private static int courseID = 0;
    private static String courseTitle;
    private static String courseCode;
    //static ID increments with every new

    public Course(int id, String title, String code)
    {
        courseID = id;
        courseTitle = title;
        courseCode = code;
        courseID++;
    }

    //****get methods*****//
    public String getCourseTitle() {return courseTitle;}
    public String getCourseCode() {return courseCode;}
}

【问题讨论】:

  • 乍一看,Course 的成员被下一个覆盖了,也许您将它们存储为静态?您可以发布课程的 sn-p 以便其他人也可以检查吗?
  • @Hababa 我编辑了原帖以添加课程课程
  • 这就是问题所在!谢谢哈巴巴

标签: java android sqlite android-studio listview


【解决方案1】:

为未来的访问者发布此答案。问题在于我的课程。我把它改成了这个,它起作用了。变量最初被声明为静态的,这就是问题所在。

public class Course
{
    private long id;
    private String code;
    private String title;

    public Course(long id, String code, String title)
    {
        this.id = id;
        this.code = code;
        this.title = title;
    }

    @Override
    public String toString() { return "Course{" + "id=" + id + ", title='" + title + '\'' + ", code='" + code + '\'' + '}'; }

    public long getId() { return id; }
    public String getCode() { return code; }
    public String getTitle() { return title; }

    public void setId(long id) { this.id = id; }
    public void setCode(String code) { this.code = code; }
    public void setTitle(String title) { this.title = title; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多