0

I have a problem with my Android application, my application is built to use ProgressDialog for showing data from calculation, but without use "sleep"

But the result of data from that calculation does not show up

I want to display the data with TextView and Listview

The message error is only the original thread that created a view hierarchy can touch its views

This is my code :

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

And here is my cariRute method :

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

    }

This my ruteListAdapter class:

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

Can anyone help me solve my problem?

joran
  • 169,992
  • 32
  • 429
  • 468
idham
  • 269
  • 2
  • 3
  • 9

2 Answers2

0

try this

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

EDIT

one more thing whenever you're updating the UI

use UI thread

like:

yourActivityName.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        ONGKOS.setText("        Ongkos : Rp."+harga);
    }
});
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • 1
    The cariRute(tv) is still being called on the other thread, so it will still cause the error when it tries to setText() the TextViews – matt5784 Jul 05 '12 at 16:14
  • thanks @matt5784 and M Moshin Naeem, very helpful, but i have one problem more, the data want to display with list adapter, if list adapter saved in thread, still error, i have updating mys question, thanks – idham Jul 05 '12 at 16:44
  • You need to learn some basic stuff of android development :) In your case `this.setListAdapter(adapter);` is also need to run on UI thread. – Mohsin Naeem Jul 05 '12 at 17:00
  • can you help me?i have try and try, but still cannot running :( – idham Jul 05 '12 at 18:15
0

You are calling cariRute(tv) from a new thread that you create, which is fine. However, inside your cariRute() method you are trying to change the text of a TextView. This sort of operation (editing existing views) has to be done on the main UI thread; you are trying to do it on a background thread, so it is giving you an error. You will have to change your code so that the alteration of the Views happens on the UI thread.

(example: have the cariRute(tv) call return the text you want to display (aka, do all the other things except actually changing the text) then use the return value to set the text on the UI thread.)

EDIT based on your explanation in comments:

You should take a look at this question. ( Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification" ) Essentially, if you are going to edit the contents of a ListView (by changing the ListAdapter) you have to call notifyDataSetChanged so the listview knows to check the data for changes.

Community
  • 1
  • 1
matt5784
  • 3,065
  • 2
  • 24
  • 42