Thread Problems on Android

0

I have listview to be populated / powered by a webservice , but only load my images with a Handler.

I want to use a Thread, but it only runs with a Handler and I do not understand why ... Can someone help me?

Here is the code (excerpt):

new Thread(new Runnable() {
            public void run() {
                do{
                    if(isOnline()){
                        initializeArrays();
                        populateList();
                        populateListView();
                    }
                }while(!isOnline());
                        progresso.dismiss();
            }   
        })start();

private void populateListView() {
        ArrayAdapter<Clube> adapter = new MyArrayAdapter();
        list = (ListView) findViewById(R.id.classificacaoGeral);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
    }
private class MyArrayAdapter extends ArrayAdapter<Clube> {
    public MyArrayAdapter() {
        super(Main.this, R.layout.list_item, clubes);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.list_item,
                    parent, false);
        }
        // Counter de position no Ranking
        if(position == 0){
            convertView.setBackgroundColor(Color.rgb(235, 235, 235));
        }else if(position == 1){
            convertView.setBackgroundColor(Color.rgb(240, 240, 240));
        }else if(position == 2){
            convertView.setBackgroundColor(Color.rgb(249, 249, 249));
        }else{
            convertView.setBackgroundColor(Color.rgb(255, 255, 255));
        }
        Clube actual = clubes.get(position);
        TextView tv = (TextView) convertView.findViewById(R.id.item_name);
        tv.setText(actual.getName());
        // Logo do clube
        ImageView imgView = (ImageView) convertView
                .findViewById(R.id.item_img);
        imgView.setImageBitmap(this.getClubeLogo(actual.getImgURL()));
        // ID de Clube de acordo com a BD
        TextView pontos = (TextView) convertView
                .findViewById(R.id.item_pontos);
        pontos.setText(actual.getPontos() +" pontos");

        return convertView;
    }

    /**
     * Metodo para fazer o load de uma imagem por um URL
     * 
     * @param src
     *            String de URL de image
     * @return Bitmap
     */
    private Bitmap getClubeLogo(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap logo = BitmapFactory.decodeStream(input);
            return logo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
asked by anonymous 08.03.2014 / 02:08

1 answer

2

Dude you can only handle View in UI Thread . Lower the data and treat it as you want in your Thread , but when this data is popular in your ListView go back to Main Thread, for this you can use a Handler previously created in the Thread UI or you can call the < a

runOnUiThread

08.03.2014 / 22:24