1. Are you ready for the Galaxy S20? Here is everything we know so far!

I am trying to fetch data from Sqlite but it stopped working unexpectedly

Discussion in 'Android Help' started by Ankur Rastogi, Jan 19, 2016.

?

Problem in displaying data in ListView

  1. Android ListView

    1 vote(s)
    100.0%
  2. Sqllite

    1 vote(s)
    100.0%
Multiple votes are allowed.
  1. Ankur Rastogi

    Ankur Rastogi Lurker
    Thread Starter

    public void fetchData() {
    dbHandler = new CourseDbHandler(this,null,null,1);
    List<CreateCourse> course_data = dbHandler.getdata();
    String[] data = new String[course_data.size()];
    int i=0;
    for(CreateCourse co : course_data)
    {
    data = co.getCourseSelected();
    i++;
    }
    courseList = (ListView) findViewById(R.id.CourselistView);
    courseAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
    courseList.setAdapter(courseAdapter);
    courseAdapter.notifyDataSetChanged();
    }

    in this method i used dbHandler.getdata method, which is as follows

    public List<CreateCourse> getdata() throws NullPointerException {
    String dbString = "";
    String course;
    String sec;

    List<CreateCourse> list = new ArrayList<>();

    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_COURSE +";";
    Cursor c = db.rawQuery(query,null);
    c.moveToFirst();

    if(c.equals(null)){
    System.out.println("NO DATA");
    return null;
    }
    else {
    do {
    course = c.getString(1);
    sec = c.getString(2);
    dbString = course+" "+sec;
    CreateCourse co = new CreateCourse();
    co.setCourseSelected(dbString);
    list.add(new CreateCourse(c.getString(1), c.getString(2)));
    }while (c.moveToNext());
    }
    db.close();
    return list;
    }

    These are the getter and setter method

    public void setCourseSelected(String courSelected) {
    courseSelected = courSelected;
    }

    public String getCourseSelected() {
    System.out.println(courseSelected);
    return courseSelected;
    }

    It gives an error unfortunately app has stopped.
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - trying fetch data
  1. Masterkirkby
    Replies:
    2
    Views:
    536
  2. dennispacito
    Replies:
    5
    Views:
    814
  3. ozth89
    Replies:
    2
    Views:
    2,105
  4. hussanhijazi
    Replies:
    0
    Views:
    777
  5. jan Graves
    Replies:
    9
    Views:
    1,541
  6. The_Chief
    Replies:
    11
    Views:
    1,633
  7. regressist
    Replies:
    0
    Views:
    1,998
  8. Mohammed Khalid
    Replies:
    5
    Views:
    2,465
  9. Gabriele Cestra
    Replies:
    0
    Views:
    1,069

Share This Page

Loading...