如何在Android中显示方法之前制作Progressdialog

时间:2012-07-05 16:06:40

标签: android multithreading progressdialog

我的Android应用程序出现问题,我的应用程序构建为使用ProgressDialog显示计算数据,但不使用“sleep”

但该计算的数据结果未显示

我想用TextView和Listview

显示数据

邮件错误为only the original thread that created a view hierarchy can touch its views

这是我的代码:

 progressDialog = ProgressDialog.show(hasilrute.this, "","Communicating",true); 

            new Thread(new Runnable() { 
                 @Override 
                 public void run() { 
                    cariRute(tv);
                    try { 

                    } catch (Exception e) { 
                    // TODO: handle exception 
                   } 
                   handler.sendEmptyMessage(0); 
                progressDialog.dismiss(); 
                 } 
           }).start(); 
          progressDialog.dismiss(); 
           Handler handle=new Handler(){ 
                 public void handleMessage(Message msg) { 
                       super.handleMessage(msg); 
                       if(msg.what == 0) { 
                         //perform some action here................ 
                       } 
                 } 
           }; 

这是我的cariRute方法:

public void cariRute(TextView t){
    String haha = tv.getText().toString().toLowerCase();
    String []getAsalTujuan = haha.split("-");
    getAsal = getAsalTujuan[0];
    getTujuan = getAsalTujuan[1];
    cari(getAsal,getTujuan);


    route = new ArrayList<rute>();
    for(int i=0;i<hasilKahir.size()-1;i++){
        a = hasilKahir.get(i);
        i++;

        b = hasilKahir.get(i);
        i--;

        hitungJarak(a, b);
        System.out.println(a+b);

        List<rute> temp = new ArrayList<rute>();
        temp = getDataRute(a,b);

        /*temp = getDataRute(a,b);
        for(int j = 0; j < temp.size(); j++){
            route.add(temp.get(j));             
        }*/

    }
    List<rute> temp = new ArrayList<rute>();
    temp = filterList(listJalan);
    route = temp;

        jrk = 0;
        for(int k = 0;k<Ljrk.size();k++){

                jrk = jrk + Ljrk.get(k);
            }
        //System.out.println(jrk);
        ongkos = 0; 
        for(int l = 0;l<Longkos.size();l++){

            ongkos = ongkos + Longkos.get(l);
            //System.out.println(ongkos);
        }

        pembulatan("####.##", jrk);
        hitungOngkos(ongkos);
        System.out.println(jrk+ongkos);

    JARAK = (TextView)findViewById(R.id.textView11);
    JARAK.setText("        Jarak : "+keluaran+" km");

    ONGKOS = (TextView)findViewById(R.id.textView12);
    ONGKOS.setText("        Ongkos : Rp."+harga);

    Longkos.clear();


    for(int x = 0; x < route.size();x++){

    }
        ListAdapter adapter = new ruteListAdapter(this, (List<? extends Map<String, String>>) route,
                R.layout.list_item_2, new String[] {
                  rute.ANGKOT, rute.JALAN }, new int[] {
                  R.id.textID, R.id.textRute });
                  this.setListAdapter(adapter);
                  //db.close();

    }

这是我的ruteListAdapter课程:

private List<rute> route;
private int[] colors = new int[] { 0x30ffffff, 0x30808080 };
@SuppressWarnings("unchecked")
public ruteListAdapter(Context context, 
    List<? extends Map<String, String>> route, 
    int resource, 
    String[] from, 
    int[] to) {
super(context, route, resource, from, to);
this.route = (List<rute>) route;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}

任何人都可以帮我解决问题吗?

2 个答案:

答案 0 :(得分:0)

试试这个

progressDialog = ProgressDialog.show(hasilrute.this, "","Communicating",true); 

    new Thread(new Runnable() { 
         @Override 
         public void run() { 
            cariRute(tv);
            youActivityName.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    progressDialog.dismiss();

                }
            });
            try { 

            } catch (Exception e) { 
            // TODO: handle exception 
           } 

         } 
   }).start(); 

修改

每当您更新用户界面时,还有一件事

使用UI线程

喜欢:

yourActivityName.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        ONGKOS.setText("        Ongkos : Rp."+harga);
    }
});

答案 1 :(得分:0)

您正在从您创建的新主题调用cariRute(tv),这很好。但是,在cariRute()方法中,您正在尝试更改TextView的文本。这种操作(编辑现有视图)必须在主UI线程上完成;你试图在后台线程上这样做,所以它给你一个错误。您将不得不更改代码,以便在UI线程上更改视图。

(示例:让cariRute(tv)调用返回要显示的文本(也就是说,除了实际更改文本之外还执行所有其他操作),然后使用返回值在UI线程上设置文本。)< / p>

根据您在评论中的解释进行编辑:

你应该看看这个问题。 (Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification")基本上,如果要编辑ListView的内容(通过更改ListAdapter),则必须调用notifyDataSetChanged,以便listview知道检查数据是否有更改。