0

I have URL String but don't know how to set text in TextView?.

ImageView fff1=(ImageView) findViewById(R.id.iop1);

TextView hol=(TextView)findViewById(R.id.mytext);
ImageLoadTask uu=new ImageLoadTask("https://maheshwaghela.com/VIPINIH/img/Mahesh/western/mimg10.jpg",fff1);
String gg=uu.url.toString();
hol.setText(gg);

I want the URL string to be set in TextView.

Here it is ImageLoadTask Class:

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadTask(String url, ImageView imageView) {

        this.url = url;
        this.imageView = imageView;

    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);
    }

}

My task is to get images from server and set to ImageView. It's fine there are no problems but then after I want to set Imageview URL in Textview where I am not able to get URL string into Textview.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
user2741987
  • 99
  • 11
  • 1
    What goes wrong? And why do you not set the text before you use the task? Using .setText() is ok. – blackapps Nov 13 '21 at 07:58
  • @blackapps I am trying to set url string to be set in textview but nothing happen. Url string not showing in TextView – user2741987 Nov 13 '21 at 08:00
  • check this https://stackoverflow.com/a/45368503/4298796 – Mona Baharlou Nov 13 '21 at 08:03
  • But why would gg contain that string? You did tell nothing about uu.url. And we dont know what uu.url has to do with it. – blackapps Nov 13 '21 at 08:03
  • @blackapps ImageloadTask is the class where I obtain images from the server and set to image view. For your consideration I hereby edit my question and include ImageLoadTask Class. – user2741987 Nov 13 '21 at 08:31
  • @monabaharlou Thanks for your link but its not meet to the my requirement – user2741987 Nov 13 '21 at 08:41
  • Hmmm.. I'm amazed that the constructor of the async task would be executed later you expected it to do. Put some Log.d() statements in your code to see. – blackapps Nov 13 '21 at 10:34
  • Put a default text in `private String url;` Like `private String url = "not an url";`. – blackapps Nov 13 '21 at 10:37
  • `but then after I want to set Imageview URL in Textview` But you do it already before downloading. Do it in onPostExecute() too. Use an extra parameter `ImageLoadTask uu=new ImageLoadTask("https://maheshwaghela.com/VIPINIH/img/Mahesh/western/mimg10.jpg",fff1, hol).execute(); ` (You forgot the .execute() in your post!). – blackapps Nov 13 '21 at 10:39

1 Answers1

0

url in the ImageLoadTask is a private data member. Create a getter of url

public String getUrl() {
    return url;
}

And use that to setText

hol.setText(uu.getUrl());

I think that is what is the problem.

bradley101
  • 723
  • 6
  • 19