简体   繁体   中英

How to change textviews & imageviews of another activity in Android

I have an activity which can take a few seconds to load its content (mainly pictures) from the cloud. Let's say this content is pictures & description from a person. The user can go to pictures & description from another person by clicking on the next button. I'd like to avoid the loading time When this button is clicked.

To do this I have two activities : firstPersonActivity for the first person content, and secondPersonActivity for the second person content. What I try to do is to load the content of the secondPersonActivity when the user is in the firstPersonActivity, so that the secondPersonActivity can be displayed "directly" (= without needing to load content from the cloud when the next button is clicked in the firstPersonActivity). But I do not succeed in doing this, I don't know how to modify the views of the secondPersonActivity layout from the firstPersonActivity class.

I tested the following code in my firstPersonActivity but it doesn't work (there is no connexion to the cloud, it's just a test to better understand how it works). R.id.first_image_second_person is the id of my imageview in the secondPersonLayout (= the layout used in the secondPersonActivity).

ImageView firstImageSecondPerson = (ImageView) findViewById(R.id.first_image_second_person);

firstImageSecondPerson.setImageResource(R.drawable.mypicture);

When I click on the next button to go from the firstPersonActivity to the secondPersonActivity, my firstImageSecondPerson imageview is not filled.

Do you see what's wrong ? Is there a better way to avoid the loading time when the user click on the next button ?

You cannot access the views of SecondActivity before its creation. If you called this activity once only then you are able to access its views by making them static.

One more Solution for this is.. Access the whole data at once and save it in static arraylist with the help of getter-setters. Then on SecondActivity set data from that arraylist.

Hope this will work.

It is not possible to change activity if activity instance is not created. In my opinion to do what You need I would go to single activity with hidden content of second person ( VIEW.INVISIBLE ) and show/hide it when it is needed.

But if second activity must be there, then create some structure for saving bitmaps in memory. In Your code sample You get picture from drawable so we are not talking about some delays, but if images are downloaded from some server then You can create some class which will have those bitmaps created from url and instance of this class can be used on any activity.

So for example Your class for caching pictures would have some method for creating bitmaps like - How to load an ImageView by URL in Android? . And those bitmaps should be saved in some Array or HashMap to have access to it, for example ( pseudo code ):

//some structure for cashing pictures in app
class PictureCache {   

  static PictureCache instance;
  final HashMap<Integer, Bitmap> bitmaps;

  //singleton
  static PictureCache getInstance(){

    if (instance==null)
      instance = new PictureCache();

    return instance;

  }

  public PictureCache(){

    bitmaps = new HashMap<>;
  }

  private Bitmap loadBitmap(String url);//#1

  public addPicture(Integer personId, String href){
     //add to hashMap 
     bitmaps.put(personId, loadBitmap(href));
  }

  public Bitmap getPicture(Integer personId){

     return bitmaps.get(personId);
  }

} 

#1 - method from How to load an ImageView by URL in Android?

Using it in first activity:

PictureCache.getInstance().addPicture(12,"http://url.to.bitmap");

Using it in second activity:

Bitmap pic = PictureCache.getInstance().getPicture(12);

Important note - above code was written here and was not tested, it shows solution concept. Important second note - using such approach with bitmaps in memory can cause to much memory usage

I don't think you can access the view before creating the activity. You can try to use Glide for caching your images and minimizing loading time.

Try this

Picasso.with(getContext()).load(R.drawable.generatedId).into(imageView);

or

imageView.setImageDrawable(ActivityCompat.getDrawable(getContext(),
                R.drawable.generatedID));`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM