Problems with Thread on Android

Asked

Viewed 114 times

0

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

I want to use a Thread, but only run with a Handler and I don’t 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;
        }
    }
  • What exactly is the problem? This code works or presents some error?

1 answer

2

Dude you can only manipulate one View in UI Thread. Download the data and treat it as you wish in your Thread, but when this data is popular in your ListView back to Main Thread, so you can use a Handler previously created in the Thread UI or you can call the method runOnUiThread.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.