Android加载listview和进度对话框

时间:2014-06-15 08:07:20

标签: android iprogressdialog

这是我加载listview项目的代码

  @SuppressLint("DefaultLocale")
   public class SearchList extends Activity {

  private ArrayList<String> founded = new ArrayList<String>();
  private OrderAdapter m_adapter;


ListView lv;

/** Called when the activity is first created. */
@SuppressLint("DefaultLocale")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchlist);
    lv = (ListView) findViewById(R.id.listView1);

    new Load().execute();

    m_adapter = new OrderAdapter(this, R.layout.itemview, founded);

    lv.setAdapter(m_adapter);
    lv.setTextFilterEnabled(true);
    }

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

    ProgressDialog progress;

    @Override
    protected void onPreExecute() {
        progress = new ProgressDialog(SearchList.this);
        progress.setMessage("loading....");
        progress.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
             try {

            for (int i = 0; i <500000; i++) {
             founded.add("String "+i);
               }

        } catch (Exception e) {

        }
            return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        // write display tracks logic here
        progress.dismiss(); // dismiss dialog
    }
}

当我运行代码时,进度对话框已经出现但是在它被解雇后我发现列表是空的没有添加任何项目我不知道是什么问题以及为什么在加载对话框后列表为空.Pls需要救命 提前谢谢。

1 个答案:

答案 0 :(得分:0)

onPostExecute中设置listadapter因为您正在使用AsyncTask来获取适配器数据并在完成AsyncTask之前设置ListAdapter所以在完成AsyncTask之后尝试设置ListAdapter 所以添加这些

 m_adapter = new OrderAdapter(this, R.layout.itemview, founded);
lv.setAdapter(m_adapter);
lv.setTextFilterEnabled(true);

onPostExecute方法中的行代替onCreate方法

 @Override
    protected void onPostExecute(Void result) {

        // write display tracks logic here
        progress.dismiss(); // dismiss dialog
     m_adapter = new OrderAdapter(YourActivity.this, R.layout.itemview, founded);
     lv.setAdapter(m_adapter);
     lv.setTextFilterEnabled(true);
    }