简体   繁体   中英

How to change an image from ImageView when I'm in the different activity

I'm working with my diploma thesis, but i can't do this one thing.

This is my MainActivity and in there you have a picture of cars. https://imgur.com/WNFG48q The User can change the car which he is using or he wants to use. There you have another activities when he changes a car. https://imgur.com/56aok4B https://imgur.com/hwITcG4 When I click "80" (Audi 80) , This site move me to the Main Activity but does not change the picture. (But I Probably know what's happen)

I searched a lot posts on stackoverflow. I found one problem. When I have only:

setContentView(R.layout.activity_home); ImageView imageView = (ImageView) findViewById(R.id.carView); imageView.setBackground(getResources().getDrawable(R.drawable.ic_audi_80));

This code moves me to the picture R.drawable.ic_audi_80, but when I have intent move me to MainActivity and load default image from android:background(...) (I tried with app:src and it's the same) .

When I haven't "setContentView(R.layout.activity_home);", Android Studio drops me the error Null object exception and marks line "imageView.setBackground....".

AudiActivity where I can choose a car of Audi's cars.

package com.example.bsk69.parkingsystem;
public class AudiActivity extends AppCompatActivity {
    ArrayAdapter<String> audiAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audi);

        String[] audiList = {"80"};
        audiAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.simple_list_item_3,audiList);
        ListView listView = (ListView) findViewById(R.id.audi_list);
        listView.setAdapter(audiAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(audiAdapter.getItem(position)=="80"){
                    setContentView(R.layout.activity_home);
                    ImageView imageView = (ImageView) findViewById(R.id.carView);
                    imageView.setBackground(getResources().getDrawable(R.drawable.ic_audi_80));
                    Intent intent = new Intent(AudiActivity.this,HomeActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
}

MainActivity.xml code where I included ImageView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorDarkPurple"
    tools:context=".HomeActivity">

    <ImageView
        android:id="@+id/carView"
        android:layout_width="202dp"
        android:layout_height="325dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/ic_focus"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
    />
</android.support.constraint.ConstraintLayout>

I need to help from you, how do I change the image in another activity using intent. I believe I have to first move to the MainActivity and then change the image in ImageView.

Thanks for help ! Regards

Here answer: AudiActivity.java

Intent intent = new Intent(AudiActivity.this,HomeActivity.class); intent.putExtra("image",R.drawable.ic_audi_80);
startActivity(intent);

MainActivity.java (onCreate(..)):

setContentView(R.layout.activity_home);
ImageView imageView = (ImageView) findViewById(R.id.carView);
int image = getIntent().getIntExtra("image",R.drawable.ic_focus);
imageView.setImageResource(image);

Thanks Reaz Murshed !

Try to save the selected car image id on SharedPreferences, then when you start your HomeActivity verify the sharedPreferences to display the car saved image id. Check android documentation about SharedPreferences and try to apply it to your code.

https://developer.android.com/reference/android/content/SharedPreferences

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