2013-09-26 2 views
0

내 응용 프로그램에서 나는 많은 데이터를 관리하고 그들을 listview에서 봅니다. 데이터가 많습니다. 활동 시작시 일부 장치가 느려지는 것이 아닌가 걱정됩니다 ... 데이터를 표시하기 전에 데이터를로드하기위한 진행 대화 상자를 추가하고 싶습니다. 는이에 대한 예제가 있나요`안드로이드 progressdialog SQLite

private void showDetails(String anno){ 
    SQLiteDatabase db = new BilancioHelper(this).getReadableDatabase(); 
    final List<Dettaglio> dettagli = new ArrayList<Euscite.Dettaglio>(12); 

    for (int i=1; i<=12; i++){ 
     String mese; 
     if (i<10){ 
      mese = "0"+i; 
     } else { 
      mese = ""+i; 
     } 

     String sql = "SELECT _Id,Data,Categoria,Note,Uscita FROM Giornate WHERE uscita>0 AND data LIKE '"+anno+"-"+mese+"%'"; 
     Cursor c = db.rawQuery(sql, null); 

     while (c.moveToNext()){ 
      Dettaglio d = new Dettaglio(); 
      d.id = c.getInt(0); 
      d.data = c.getString(1); 
      d.categorie = (c.getString(2) != null) ? c.getString(2) : ""; 
      d.note = (c.getString(3) != null) ? c.getString(3) : ""; 
      d.uscite = c.getFloat(4); 


      dettagli.add(d); 
     } 
     c.close(); 
    } 

    db.close(); 

    ListAdapter adapter = new ArrayAdapter<Dettaglio>(this, R.layout.dettaglio_row_uscite, R.id.tv_entrata, dettagli){ 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = super.getView(position, convertView, parent); 

      Log.v("Bilancio", "Creo elemento in posizione "+position); 


      TextView tvData, tvNote, tvUscite, tvId, tvCategorie; 
      tvData = (TextView) row.findViewById(R.id.tv_data_uscita); 


      tvUscite = (TextView) row.findViewById(R.id.tv_entrata); 
      tvCategorie = (TextView) row.findViewById(R.id.tv_cat_uscite); 
      tvNote = (TextView) row.findViewById(R.id.tv_note_us); 

      tvId = (TextView) row.findViewById(R.id.tv_id_uscite); 

      Dettaglio d = dettagli.get(position); 
      tvData.setText(d.data+""); 
      tvNote.setText(d.note+""); 
      tvCategorie.setText(d.categorie+""); 

      Double value = Double.valueOf(d.uscite); 
      NumberFormat formatter = NumberFormat.getCurrencyInstance(); 
      String uscite = formatter.format(value); 
      tvUscite.setText(uscite+""); 

      tvId.setText(d.id+""); 
      return row; 
     } 
    }; 
    lista.setAdapter(adapter); 

답변

0

당신은 AsyncTasks http://developer.android.com/reference/android/os/AsyncTask.html

를 검색하는 것은 매우 간단합니다 : 당신이 슈퍼 클래스의 같은 AsyncTask를 사용하여 클래스를 구현하는 세 가지 방법 오버라이드 (override) :

onPreExecute() -method는 다음과 같이 표시해야합니다. ProgressDialog

doInBackground(Context... arg0) -method는 dat 근심.

onPostExecute(String result) -method 목록에 데이터를두고 코드는 두 가지로 방법을 분할한다에서 ProgressDialog

을 닫습니다 : 첫 번째는 후 db.close();과 모든 것을 처음부터 (의이 getData()를 부르 자) 그 두 번째 방법 (예를 showData()에 대한)

그럼 당신은이 같은 활동의 클래스 생성 :

protected class LoadDataTask extends AsyncTask<Context, Integer, String> 
{ 
ProgressDialog myLoadingDialog; 

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

@Override 
protected String doInBackground(Context... arg0) 
{ 
    getData(); 
    return null; 
} 

@Override 
protected void onPostExecute(String result) 
{ 
    showData(); 
    myLoadingDialog.dismiss(); 
    super.onPostExecute(result); 
} 
} 

를 VARI을 ​​넣어 두 가지 방법 모두에서 필요한 메서드 바깥에 있습니다.

+0

내가 입력 한대로 AsyncTask 내 코드에서 ?? 도울 수 있습니까? –

+0

@AndreaMalini 내 대답을 – Day

+0

업데이트했습니다. 클래스를 입력했지만 2와 같이 코드를 분할하는 법을 모릅니다 ... –

0

또는 CursorLoader : Loading Data in the Background. 데이터를 변경하면 다시로드 할 수 있습니다.

백그라운드 작업을 수행하는 동안 진행 상황을 표시하는 한 가지 방법은 AsyncTask를 사용하는 것입니다. 또 다른 방법은 앱이 처음 시작할 때 보이지 않는 (android : visibility = "gone")을 시작하는 ProgressBar 위젯을 사용하는 것입니다. 데이터로드를 시작하면 표시되도록 설정합니다 ((android : visibility = "visible"). 데이터로드가 완료되면 "사라짐"으로 설정합니다. "사라짐"보다 "사라짐" 사라짐 "은 레이아웃 계산에 포함되지 않은 것처럼보기 계산에서 위젯을 제거합니다."보이지 않음 "은 위젯을 표시하지 않지만 위젯이 차지하는 공간을 보여줍니다.