简体   繁体   English

无法显示 SQLite 表中的 ListView 元素

[英]Trouble Displaying ListView Elements from SQLite Table

I'm struggling to pull elements from an SQLite table in Android Studio and display the elements in a ListView.我正在努力从 Android Studio 中的 SQLite 表中提取元素并在 ListView 中显示这些元素。 The area where I believe the error to be in is when I set the List adapter to my array.我认为错误所在的区域是当我将 List 适配器设置为我的数组时。

First, here is the code in my SQHelper class.首先,这是我的 SQHelper 类中的代码。 It retrieves the list of items to be displayed.它检索要显示的项目列表。

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

NOTE that the function first prints each element that is added, then outputs the whole course list before returning it.请注意,该函数首先打印添加的每个元素,然后在返回之前输出整个课程列表。 This is important for the output of the log.这对于日志的输出很重要。

Second is the onClickListener for when one tries to add an element to this list.第二个是 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();
        });

Finally, the function DisplayCourses() which is called above.最后是上面调用的函数 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();
}

As mentioned, the SQHelper function displays each element as it is added, then prints the entire course array.如前所述,SQHelper 函数显示添加的每个元素,然后打印整个课程数组。 Here is the output (LOGCAT) when I try to insert a new course.这是我尝试插入新课程时的输出 (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**

Note that as classes are added, their IDs, titles, and codes seem identical.请注意,随着类的添加,它们的 ID、标题和代码看起来是相同的。 But when the entire list is printed, they are all the same.但是当打印整个列表时,它们都是一样的。 This leads to the list looking like the following picture.这导致列表看起来像下图。

在此处输入图片说明

So to sum this up, I am trying to display a list of elements in a listView from an SQLite table.总而言之,我试图从 SQLite 表的 listView 中显示元素列表。 When inserting elements into the array to be passed to my adapter, the course attributes that are added somehow don't add up.当将元素插入到要传递给我的适配器的数组中时,以某种方式添加的课程属性不会相加。

Note that the ListView adapter is not the issue;请注意,ListView 适配器不是问题; I have tested it with a hard coded list of courses, and displayed them all uniquely.我已经用硬编码的课程列表对其进行了测试,并将它们全部显示出来。

Thank you for your time!感谢您的时间!

EDIT: Here is the course class编辑:这是课程

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

Posting this answer for future visitors.为未来的访问者发布此答案。 The issue lied in my Course class.问题出在我的课程中。 I changed it to this, and it worked.我把它改成了这个,它奏效了。 The variables were originally declared static, which was the issue.变量最初被声明为静态,这就是问题所在。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM