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

Share This Page

Loading...