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?