从数据库加载listview时加载对话框

时间:2013-08-09 00:46:16

标签: android sqlite android-listview android-asynctask simplecursoradapter

我有问题.. 这是代码:

private final class AsyncSenderAll extends AsyncTask<SimpleCursorAdapter, Void, SimpleCursorAdapter>{

        @Override
        protected void onPreExecute(){
            super.onPreExecute();


            pd = new ProgressDialog(PilotLogbook_viewdrafts.this);
            pd.setTitle("Drafts");
            pd.setMessage("Please wait, refreshing drafts...");
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pd.show();
        }

        @Override
        protected SimpleCursorAdapter doInBackground(SimpleCursorAdapter... params) {
            DBAdapter DBHelper = new DBAdapter(PilotLogbook_viewdrafts.this);
            DBHelper.open();
            Cursor cursor = DBHelper.getAllDrafts();

            // The desired columns to be bound
              @SuppressWarnings("static-access")
            String[] columns = new String[] {

                DBHelper.KEY_DATE,
                DBHelper.KEY_PIC_NAME,
                DBHelper.KEY_AIRCRAFT_REGISTRATION,
                DBHelper.KEY_DEPARTURE_PLACE,
                DBHelper.KEY_ARRIVAL_PLACE
              }; 


              // the XML defined views which the data will be bound to
              int[] to = new int[] {

                R.id.lblLayout_Draft_Date,
                R.id.lblLayout_Draft_PIC_Name,
                R.id.lblLayout_Draft_Aircraft_Registration,
                R.id.lblLayout_Draft_Departure_Place,
                R.id.lblLayout_Draft_Arrival_Place
              };

              // create the adapter using the cursor pointing to the desired data
              //as well as the layout information
                dataAdapter = new SimpleCursorAdapter(
                this, R.layout.layout_pilotlogbook_viewdrafts,
                cursor,
                columns,
                to,
                0);

            return dataAdapter;
        }

        @Override
        protected void onPostExecute(SimpleCursorAdapter dataAdapter){
            super.onPostExecute(dataAdapter);
            ListView listView = (ListView) findViewById(R.id.listAllDrafts);
              // Assign adapter to ListView
              listView.setAdapter(dataAdapter); 


              listView.setOnItemClickListener(new OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> listView, View view,
                     int position, long id) {
                   Cursor cursor = (Cursor) listView.getItemAtPosition(position);
                   int RowID = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
               Bundle bundle = new Bundle();
               bundle.putInt("RowID",RowID);
               Intent newIntent = new Intent(getApplicationContext(), PilotLogbook_draftdetails.class);
               newIntent.putExtras(bundle);
               startActivityForResult(newIntent, 0);
               finish();

                   }  
              });



            pd.dismiss();




        }

    }

我想在listview从数据库加载数据时使用加载对话框。问题是我得到了

The constructor SimpleCursorAdapter(PilotLogbook_viewdrafts.AsyncSenderAll, int, Cursor, String[], int[], int) 
 is undefined

dataAdapter = new SimpleCursorAdapter(
                this, R.layout.layout_pilotlogbook_viewdrafts,
                cursor,
                columns,
                to,
                0);

我不知道如何解决它。你能帮我解决这部分代码吗?提前致谢! :d

2 个答案:

答案 0 :(得分:1)

试试这个

dataAdapter = new SimpleCursorAdapter(
                PilotLogbook_viewdrafts.this, R.layout.layout_pilotlogbook_viewdrafts,
                cursor,
                columns,
                to,
                0);

答案 1 :(得分:0)

您在错误的位置使用this,在这种情况下this表示AsyncSenderAll类,但适配器需要上下文,因此请尝试将Activity类放在那里。例如MainActivity.this

dataAdapter = new SimpleCursorAdapter(
                MainActivity.this, R.layout.layout_pilotlogbook_viewdrafts,
                cursor,
                columns,
                to,
                0);

希望这有帮助。