Pergunta

I got a List View in my application in which I am displaying the data through Base Adapter. Their is two problems I am facing and referred few posts like but all suggested same procedure i followed.

Issues are

  1. I am trying to download the image from the URL given from JSON. everything is working smoothly but the image never get set to Image View.

  2. I bound Text to Speech on click event of button in Base Adapter class and freed it in onDestroy of of the java class but still I get a error in Log for it stating this and application crashes. Here in log erroe line no 55 is the first statement of onDestroy.

Here is my code

Java File

public class DisplayWeather extends Activity {

    String city, date, maximumTemp, minimumTemp, description, weatherImageUrl;
    ListView weatherList;
    List <Bean> bean;
    Bitmap myBitmap, newBitmap;
    CustomBaseAdapter baseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_weather);

        bean = new ArrayList<Bean>();
        weatherList = (ListView) findViewById(R.id.lvWeather);

        for(int i=0; i<WeatherHome.arrayList.size(); i++)
        {
            .
                    .

        }


        weatherList.setAdapter(new CustomBaseAdapter(this, bean));
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (baseAdapter.tts != null)
        {
            baseAdapter.tts.stop();
            baseAdapter.tts.shutdown();
        }
        super.onDestroy();
    }

Base Adapter class

public class CustomBaseAdapter extends BaseAdapter implements OnInitListener {
    Context context;
    List<Bean> bean;
    ImageView weatherImage;
    TextView weatherDate, weatherCity, weatherMinimum, weatherMaximum, weatherDescription;
    Button buttonSpeak;
    String citySpeak, dateSpeak, descriptionSpeak, maximumSpeak, minimumSpeak, weatherURL;
    TextToSpeech tts;
    Bean userBean;
    Bitmap myBitmap;

    public CustomBaseAdapter(Context context, List<Bean> bean) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.bean = bean;
        tts = new TextToSpeech(context, null);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return bean.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return bean.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return bean.indexOf(getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.custom_base_adapter, null);
            weatherImage = (ImageView) convertView.findViewById(R.id.displayImage);
convertView.findViewById(R.id.displayDate);
            buttonSpeak = (Button) convertView.findViewById(R.id.Speak);


        }

        weatherURL = userBean.getImageUrl();

        new ImageDownload().execute();

        Log.i("Executing Rest Line>>>", "Skippedddddd");
        buttonSpeak.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            String cityName = weatherCity.getText().toString(); 
            String dateValue = weatherDate.getText().toString();
            String maximumValue = weatherMaximum.getText().toString();
            String minimumValue = weatherMinimum.getText().toString();
            String descriptionValue = weatherDescription.getText().toString();

            citySpeak = "Temprature for city "+cityName+"";
            dateSpeak = " on Date "+dateValue+"";
            maximumSpeak = "will be Maximum upto "+maximumValue+" degree ";
            minimumSpeak = " and Minimum upto"+minimumValue+" degree ";
            descriptionSpeak = "and The atmosphere seems to be "+descriptionValue+"";

            speakTempratureValues();
            }
        });

        return convertView;
    }

    private class ImageDownload extends AsyncTask<String, Void, Bitmap>{

        protected Bitmap doInBackground(String... arg0){

            try{
                Log.e("src",weatherURL);
                URL url = new URL(weatherURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                myBitmap = BitmapFactory.decodeStream(input);       
                Log.e("Bitmap","returned");
                return myBitmap;
            }
            catch(Exception e){
                e.printStackTrace();
                return null;
            }

        }

        protected void onPostExecute(Bitmap result){
             if(result!=null)
             {
                Log.i("OnPost>>>", ""+result);
                weatherImage.setImageBitmap(result);
             }

        }
    }

    protected void speakTempratureValues() {
        // TODO Auto-generated method stub
        tts.setSpeechRate(-4);
        tts.speak(citySpeak, TextToSpeech.QUEUE_FLUSH, null);
        tts.speak(dateSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak(maximumSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak(minimumSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak(descriptionSpeak, TextToSpeech.QUEUE_ADD, null);
        tts.speak("Thank You", TextToSpeech.QUEUE_ADD, null);
    }


    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
        if(status==TextToSpeech.SUCCESS){
            int result = tts.setLanguage(Locale.getDefault());

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            }
            else{

                speakTempratureValues();
            }
        }
        else{
            Log.e("TTS", "Initialization Failed");
        }
    }

}
Foi útil?

Solução 2

What Louis suggested is the correct way. I am sure this will work. The reason why it's not working for you is not sure but try this way:

Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try{
                        Log.e("src",weatherURL);
                        URL url = new URL(weatherURL);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setDoInput(true);
                        connection.connect();
                        InputStream input = connection.getInputStream();
                        myBitmap = BitmapFactory.decodeStream(input);       
                        Log.e("Bitmap","returned"+myBitmap);

                        if(myBitmap!=null)
                     {
                        Log.i("OnPost>>>", ""+myBitmap);
                        weatherImage.setImageBitmap(myBitmap);
                     }
                    }
                    catch(Exception e){
                        e.printStackTrace();

                    }

                }
            };

            Thread t = new Thread(runnable);
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Probably, the start method was missing for you and you might not have called it. I hope this work.

Outras dicas

It looks like maybe you're returning your convertView in the getView method before your AsyncTask is finished downloading the image. Can you use a thread instead, and use the Thread join method so that your app waits for the image to be downloaded? For an AsyncTask you usually use a progress dialog until the task is finished, but I don't think you can do that inside of an adapter.

How about replacing this:

new ImageDownload().execute();

with this:

new Thread(new Runnable() {
public void run() {
  try{
            Log.e("src",weatherURL);
            URL url = new URL(weatherURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);       
            Log.e("Bitmap","returned");
            return myBitmap;
        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }}
}).join();

and then obviously get rid of the ImageDownload class. I'm just throwing this at you, didn't test it or anything. I think that should get you closer.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top