mardi 18 août 2015

Populate listView from sqlite data with AsyncTask

I am new to android and i know only some few stuffs. I need to learn more. I am using AsyncTask for my Listview. I am populating my listview with data from sqlite database. The problem here is that the Listview items cannot be displayed. It always just display the progress dialog and it's not getting any data behind it. I don't know what's wrong with the code. I don't know where I'm wrong here. Please help me with this one please.

Db method

public List<Sport> getSports() {

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

            Cursor cursor = database.rawQuery("SELECT * FROM sports", null);
            cursor.moveToFirst();

            while (!cursor.isAfterLast()) {

                Sport s = new Sport();
                s.setId(cursor.getString(0));
                s.setSport(cursor.getString(1));

                list.add(s);
                cursor.moveToNext();
            }
            cursor.close();
            return list;
     }

On my onCreate method:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            search_view = (SearchView) findViewById(R.id.search_view);

            new MyAsynch().execute();

}

Adapter class

 private class Adapter extends ArrayAdapter<Sport>  {

            private ArrayList<Sport> originalList;
            private ArrayList<Sport> objects;

            public Adapter(Context context, List<Sport> objects) {
                super(context, 0, objects);

                this.objects = new ArrayList<Sport>();
                this.objects.addAll(objects);
                this.originalList = new ArrayList<Sport>();
                this.originalList.addAll(objects);

            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.sports, parent, false);
                }

                TextView id = (TextView) convertView.findViewById(R.id.id);
                TextView sport = (TextView) convertView.findViewById(R.id.sport);

                Sport s = sports.get(position);
                id.setText(s.getId());
                sport.setText(s.getSport());

                return convertView;
            }
     }

AsyncTask Class

  private class MyAsynch extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
         super.onPreExecute();
            pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("Loading ...");
            pd.setIndeterminate(false);
            pd.show();
        }

        @Override
        protected Void doInBackground(Void...strings) { // run time intensive task in separate thread

                    databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
                    databaseAccess.open();
                    sportsList = databaseAccess.getSports();
                    databaseAccess.close();

                    return null;
                }
        }

        protected void onPostExecute(String result) {

        // Give the data to you adapter from here,instead of the place where you gave it earlier
            lv = (ListView) findViewById(R.id.listView);
            adapter = new Adapter(this, sportsList );
            lv.setAdapter(adapter);
            pd.dismiss();
        }
  }

Aucun commentaire:

Enregistrer un commentaire