麻烦在从SQLite表中显示ListView元素时出现问题。

huangapple go评论76阅读模式
英文:

Trouble Displaying ListView Elements from SQLite Table

问题

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

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

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

public class Course {
    private static int courseID = 0;
    private static String courseTitle;
    private static String courseCode;

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

    public String getCourseTitle() {
        return courseTitle;
    }

    public String getCourseCode() {
        return courseCode;
    }
}
英文:

I'm struggling to pull elements from an SQLite table in Android Studio and display the elements in a ListView. The area where I believe the error to be in is when I set the List adapter to my array.

First, here is the code in my SQHelper class. It retrieves the list of items to be displayed.

 public ArrayList&lt;Course&gt; getCourses()
{
SQLiteDatabase db = this.getReadableDatabase();
ArrayList&lt;Course&gt; courses = new ArrayList&lt;&gt;();
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(&quot;ID: &quot;+id+&quot;\nTITLE: &quot;+title+&quot;\nCODE&quot;+code);
courses.add(new Course(id,title,code));
System.out.println(&quot;SUCCESFULLY ACCESSED SQ TABLE; ADDING COURSES&quot;);
} while(cursor.moveToNext());
}
db.close();
Log.d(&quot;SQHELPER&quot;,&quot;COURSES:&quot;);
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.

saveButton.setOnClickListener(save_view -&gt;
{
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(),&quot;Saved!&quot;,Toast.LENGTH_SHORT).show();
getDialog().dismiss();
});

Finally, the function DisplayCourses() which is called above.

public void DisplayCourses()
{
ArrayList&lt;Course&gt; 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. Here is the output (LOGCAT) when I try to insert a new course.

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. But when the entire list is printed, they are all the same. This leads to the list looking like the following picture.

麻烦在从SQLite表中显示ListView元素时出现问题。

So to sum this up, I am trying to display a list of elements in a listView from an SQLite table. 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; 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;}
}

答案1

得分: 0

将此答案发布供将来的访问者使用。问题出在我的Course类中。我将它更改为以下内容,然后它就起作用了。变量最初被声明为静态,这就是问题所在。

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; }
}
英文:

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 &quot;Course{&quot; + &quot;id=&quot; + id + &quot;, title=&#39;&quot; + title + &#39;\&#39;&#39; + &quot;, code=&#39;&quot; + code + &#39;\&#39;&#39; + &#39;}&#39;; }
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; }
}

huangapple
  • 本文由 发表于 2020年10月16日 08:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64381388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定