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