使用AsyncTask从sqlite数据填充listView

时间:2015-08-19 04:01:02

标签: sqlite android-listview android-asynctask

我是Android新手,我只知道一些东西。我需要了解更多。我在我的Listview中使用AsyncTask。我用来自sqlite数据库的数据填充我的listview。这里的问题是Listview项目无法显示。它始终只显示进度对话框,并且它后面没有任何数据。我不知道代码有什么问题。我不知道我在哪里错了。

Db方法

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

在我的onCreate方法上:

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

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

            new MyAsynch().execute();

}

适配器类

 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类

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

1 个答案:

答案 0 :(得分:0)

我认为这个问题是:

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

在行适配器= new Adapter(this,sportsList); 当你发送它时,你发送asyctask上下文,这就是一个问题。

应该是:

    adapter = new Adapter(getApplicationContext(), sportsList );

进行更改然后告诉我它是否会出错。

此致