0

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://i.stack.imgur.com/KHvzu.jpg 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://i.stack.imgur.com/0qM0U.jpg https://i.stack.imgur.com/S65rM.jpg 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

133794m3r
  • 5,028
  • 3
  • 24
  • 37
  • https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – Reaz Murshed Aug 22 '19 at 23:24
  • https://stackoverflow.com/questions/24706348/what-is-setcontentviewr-layout-main – Reaz Murshed Aug 22 '19 at 23:24
  • I cleaned up your typoes and minor grammatical mistakes. I didn't mess with the source code as I'm not a java guy. Also in the future you don't use
     on stackoverflow. It uses markdown. See the following page for help when working with markdown instead of bbcode/markup. https://stackoverflow.com/editing-help
    – 133794m3r Aug 23 '19 at 00:17
  • Thank you very much.I'm very happy, because It's now working ! Your links explained me something which I wasn't sure, because before I tried with "intent.putExtra(...)" –  Aug 23 '19 at 00:31

2 Answers2

0

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 !

0

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

George Hoss
  • 121
  • 5
  • It's not stupid, because text is saved in SharedPreferences. When I back to application, i can have the same car which i chosen. I try to implement this thing, Thanks –  Aug 23 '19 at 07:57
  • I was "surfing" on the internet and I found information about that. People wrote It's not good idea to save it in SharePreferences and user should save image in sdcard. I don't know what i should do, because my logic say me it's stupid save image from program. It isn't custom image from users. –  Aug 25 '19 at 20:13