I am making a mp3 player app , in my main activity I am showing the list of all songs in recycler view and when user click on the song I am trying to send entire array list of songs to my player activity , where I can work for with next and previous songs play , but my app crashes when click the song
Process: com.choudhary.musicplayer, PID: 8686
java.lang.RuntimeException: Parcel: unable to marshal value com.choudhary.musicplayer.AudioModel#a0de380
at android.os.Parcel.writeValue(Parcel.java:1667)
at android.os.Parcel.writeList(Parcel.java:966)
at android.os.Parcel.writeValue(Parcel.java:1614)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:878)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1588)
at android.os.Bundle.writeToParcel(Bundle.java:1233)
at android.os.Parcel.writeBundle(Parcel.java:918)
at android.content.Intent.writeToParcel(Intent.java:9987)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:3636)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1675)
at android.app.Activity.startActivityForResult(Activity.java:4651)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:597)
at android.app.Activity.startActivityForResult(Activity.java:4609)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:583)
at android.app.Activity.startActivity(Activity.java:4970)
at android.app.Activity.startActivity(Activity.java:4938)
at com.choudhary.musicplayer.MusicAdapter$1.onClick(MusicAdapter.java:54)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
My Adapter's OnBind method :--
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(arrayList.get(position).getaName());
holder.album.setText(arrayList.get(position).getaAlbum());
holder.imageView.setImageBitmap(BitmapFactory.decodeFile(arrayList.get(position).getAlbumart()));
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in = new Intent(context,PlayerActivity.class);
in.putExtra("SONG",arrayList.get(position).getaName());
in.putExtra("PATH",arrayList.get(position).getaPath());
in.putExtra("ALBUM",arrayList.get(position).getaAlbum());
in.putExtra("LIST",arrayList);
in.putExtra("POSITION", arrayList.get(position).toString());
context.startActivity(in);
}
});
}
my Player Activity :---
public class PlayerActivity extends AppCompatActivity {
TextView songanme, songAlbum,duration,movetime;
ImageView playbutton,nextbtn,previousbtn;
SeekBar seekBar;
MediaPlayer mediaPlayer ;
ArrayList<AudioModel> list;
int CURRENT_POSITION ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
songanme = findViewById(R.id.music_name_pl);
movetime = findViewById(R.id.move_time);
seekBar = findViewById(R.id.seekBar);
songAlbum = findViewById(R.id.music_album_pl);
duration = findViewById(R.id.duration);
playbutton = findViewById(R.id.play_btn_pl);
nextbtn = findViewById(R.id.next_btn_pl);
previousbtn = findViewById(R.id.previous_pl);
list = new ArrayList<>();
songanme.setSelected(true);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
list = (ArrayList) bundle.getParcelableArrayList("LIST");
}
Let's assume that you want to pass an ArrayList of the Song class from Activity1 to Activity2.
1- The Song class should implement the Serializable class.
It would be something like that..
import java.io.Serializable;
public class Song implements Serializable {
String name;
String album;
public Song(String name, String album) {
this.name = name;
this.album = album;
}
}
2-In Activity1 pass your array list object as an extra to Ativity2
ArrayList<Song> songs= new ArrayList();
songs.add(new Song("song1","album1"));
songs.add(new Song("song2","album2"));
songs.add(new Song("song3","album3"));
Intent intent=new Intent(this,Activity2.class);
intent.putExtra("songs",songs);
startActivity(intent);
3- Finally receive the array list with getSerializableExtra in Activity2
ArrayList<Song> songs = (ArrayList<Song>) getIntent().getSerializableExtra("songs");
Log.i("HINT", "" + songs.size());
You can create a singleton class for sharing your ArrayList across various components of android. Sample code for the singleton class is described below-
public class SongBank
{
private static SongBank instance;
private ArrayList<Song> songsArrayList;
private SongBank(Context context)
{
// You can do any stuff if you want here
}
// create getter and setter methods for your arrayList
public void setSongsList(ArrayList<Song> songs)
{
if(songs!=null)
{
this.songsArrayList=songs;
}
}
public ArrayList<Song> getSongsList()
{
return this.songsArrayList;
}
public static SongBank getInstance(Context context)
{
if(instance==null)
{
instance=new SongBank(context);
}
return instance;
}
}
The instance of this singleton class can be called across various activities or fragments and you can also change the Arraylist value across different activities if you want. You can also call this class and get the list in your music service without worrying about serialization.
Related
I have recyclerview with each item holding some buttons and text.
I am setting my onViewClickListener in the ViewHolder. I have a Room database initialized in the MainActivity.
In terms of good app architecture, how should I change the data in my database once my OnClickListener triggers?
Do I pass the database as a parameter to the adapter then the view holder?
OR
Do I get the database through a getDatabase method that I implement?
What is the best way to go about this? I am open to any suggestion/design pattern.
How is something like this generally handled?
Best practise is to keep the database in your AppCompatActivity.
If you display database data in that recyclerView you should create an ArrayList filled with those data and pass it to the recyclerViewAdapter in the constructor like MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(myData)
Then everytime you change something from that list (from your activity) you just call notify method based on your action. General one is myRecyclerViewAdapter.notifyDataSetChanged(). But with lot data it's more efficient to use more specific notify methods.
And if the database should change based on some event in recyclerView, for example by the onViewClickListener. You should create an interface in which you would pass the change and then apply the change to the database in your AppCompatActivity.
You should keep your database (and strongly advise you to use the data with ViewModel) in your activity. You can make changes on data of recyclerview item by using weakreference and interface. Here is example of activity and adapter.
public class MyActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyObjectAdapter adapter;
private final List<MyObject> myObjectList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//define your views here..
setAdapter();
getData();
}
private void setAdapter(){
adapter = new MyObjectAdapter(myObjectList, new
MyObjectAdapter.IObjectClickListener() {
#Override
public void onButtonOneClick(MyObject myObject) {
//do button one operation with object item;
}
#Override
public void onButtonTwoClick(MyObject myObject) {
//do button two operation with object item;
}
});
}
private void getData(){
//Get your data from database and pass add them all into our
//myObjectList
myObjectList.addAll(objects); //Suppose objects come from database
adapter.notifyDataSetChanged();
}
}
And adapter class is like
public class MyObjectAdapter extends RecyclerView.Adapter<MyObjectAdapter.ObjectViewHolder> {
private final List<MyObject> myObjects;
private final IObjectClickListener listener; //defined at the bottom
public MyObjectAdapter(List<MyObject> myObjects, IObjectClickListener listener) {
this.myObjects = myObjects;
this.listener = listener;
}
/*
..
Other methods of adapter are here
..
*/
public class ObjectViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Button buttonOne, buttonTwo;
private WeakReference<IObjectClickListener> reference;
public ObjectViewHolder(#NonNull View itemView) {
super(itemView);
//define your views here
reference = new WeakReference<>(listener);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == buttonOne){
reference.get().onButtonOneClick(myObjects.get(getAdapterPosition()));
} else if (v == buttonTwo){
reference.get().onButtonTwoClick(myObjects.get(getAdapterPosition()));
}
}
public interface IObjectClickListener{
void onButtonOneClick(MyObject myObject);
void onButtonTwoClick(MyObject myObject);
}
}
Main Goal:-
I have a list of sports news. Each item contains a sport name and some info. Clicking on it will show the latest news regarding that particular sport. The user has the option to swipe to dismiss a news, if they don't want it in the list or they can also drag and drop it, for example, if they want to see some news on top of others.
Each item in the list is represented programmatically as a Sport.java object.
I want to retain the state of the list upon device orientation changes.
What I've tried:-
For the list, I have an arraylist of sport objects (ArrayList). I learned that to save a list of custom objects, they objects themselves need to be Parcelable. For this, I implemented the Parcelable.java interface like this:
package com.example.android.materialme;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
/**
* Data model for each row of the RecyclerView.
*/
class Sport implements Parcelable {
//Member variables representing the title and information about the sport
private String title;
private String info;
private String detail;
private final int imageResource;
/**
* Constructor for the Sport data model
* #param title The name if the sport.
* #param info Information about the sport.
*/
Sport(String title, String info, String detail, int imageResource) {
this.title = title;
this.info = info;
this.detail = detail;
this.imageResource = imageResource;
}
protected Sport(#NonNull Parcel in) {
title = in.readString();
info = in.readString();
detail = in.readString();
imageResource = in.readInt();
}
public static final Creator<Sport> CREATOR = new Creator<Sport>() {
#Override
public Sport createFromParcel(Parcel in) {
return new Sport(in);
}
#Override
public Sport[] newArray(int size) {
return new Sport[size];
}
};
String getTitle() {
return title;
}
String getInfo() {
return info;
}
int getImageResource(){
return imageResource;
}
String getDetail(){
return detail;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title);
parcel.writeString(info);
parcel.writeString(detail);
parcel.writeInt(imageResource);
}
}
and then I used
outState.putParcelableArrayList(KEY, sportsList);
but, this doesn't work. The screen is just blank upon rotating device.
I tried debugging the app and found that the arraylist was being passed correctly with the data intact, it's just that the app is not being able to display it for some reason.
Also, the implementation of the fab button is so that it resets the whole list to its initial condition upon click. The fab works normally but if the orientation is changed once, it stops working (app doesn't crash). Changing the orientation back also doesn't fix the fab. So, to get the list again for any other test, I have to rerun the entire app.
Complete Code:-
MainActivity.java
package com.example.android.materialme;
import android.content.res.TypedArray;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
//Member variables
private RecyclerView mRecyclerView;
private ArrayList<Sport> mSportsData;
private SportsAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(view -> resetSports());
//Initialize the RecyclerView
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Set the Layout Manager
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Initialize the ArrayLIst that will contain the data
mSportsData = new ArrayList<>();
//Initialize the adapter and set it ot the RecyclerView
mAdapter = new SportsAdapter(this, mSportsData);
mRecyclerView.setAdapter(mAdapter);
initializeData(savedInstanceState);
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
int from = viewHolder.getAdapterPosition();
int to = target.getAdapterPosition();
Collections.swap(mSportsData, from, to);
mAdapter.notifyItemMoved(from, to);
return true;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mSportsData.remove(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
});
helper.attachToRecyclerView(mRecyclerView);
}
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("state", mSportsData);
}
/**
* Method for initializing the sports data from resources.
*/
private void initializeData(Bundle savedInstanceState) {
if(savedInstanceState!=null){
mSportsData.clear();
mSportsData = savedInstanceState.getParcelableArrayList("state");
} else {
//Get the resources from the XML file
String[] sportsList = getResources().getStringArray(R.array.sports_titles);
String[] sportsInfo = getResources().getStringArray(R.array.sports_info);
String[] sportsDetail = getResources().getStringArray(R.array.sports_detail);
TypedArray sportsImageResource = getResources().obtainTypedArray(R.array.sports_images);
//Clear the existing data (to avoid duplication)
mSportsData.clear();
//Create the ArrayList of Sports objects with the titles and information about each sport
for (int i = 0; i < sportsList.length; i++) {
mSportsData.add(new Sport(sportsList[i], sportsInfo[i], sportsDetail[i], sportsImageResource.getResourceId(i, 0)));
}
sportsImageResource.recycle();
}
//Notify the adapter of the change
mAdapter.notifyDataSetChanged();
}
public void resetSports(){
initializeData(null);
}
}
App Images:-
#1 Initial List
#2 Changed List (Card #2 for sport basketball is swiped)
Orientation change to landscape:-
Even though the question is 4 months old and you probably don't need the answer anymore:
The problem is that you initialize the adapter with mSportsData, but reassign another value to the variable later in initializeData(). The ArrayList bound to the adapter is still the empty one it got initialized with.
A way to solve it would be to initialize mSportsData with either a new ArrayList if savedInstanceState is null or else the saved value, and to call initializeData only if savedInstanceState is null. You can remove the argument and therefore the if from initalizeData() completely.
// Initialize the ArrayList that will contain the data.
if (savedInstanceState == null) {
mSportsData = new ArrayList<>();
} else {
mSportsData = savedInstanceState.getParcelableArrayList("state");
}
//Initialize the adapter and set it ot the RecyclerView (nothing changed here)
mAdapter = new SportsAdapter(this, mSportsData);
mRecyclerView.setAdapter(mAdapter);
if (savedInstanceState == null) {
initializeData();
}
Remove this function call from OnCreate initializeData(savedInstanceState); and call it in OnResume
I couldn't get it working with the Parcelable way so what I tried doing (and it worked) was save all the info contained inside all the sport objects in separate lists and put those in the bundle instead.
In the initialiseData(), I just used these lists then to create a new list of sport objects with the exact data.
This shouldn't be the way of doing it but my app just doesn't loads the saved list if it's passed as a parcelable and I don't know why.
Here's the working code for now:-
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
ArrayList<String> titles = new ArrayList<>();
ArrayList<String> infos = new ArrayList<>();
ArrayList<String> details = new ArrayList<>();
ArrayList<Integer> imgs = new ArrayList<>();
for(Sport sport: mSportsData){
titles.add(sport.getTitle());
infos.add(sport.getInfo());
details.add(sport.getDetail());
imgs.add(sport.getImageResource());
}
outState.putStringArrayList("title", titles);
outState.putStringArrayList("info", infos);
outState.putStringArrayList("detail", details);
outState.putIntegerArrayList("img", imgs);
}
private void initializeData(Bundle savedInstanceState) {
boolean hasData = false;
if(savedInstanceState!=null){
if(savedInstanceState.containsKey("title")){
//Get the resources from the bundle
ArrayList<String> sportsList = savedInstanceState.getStringArrayList("title");
ArrayList<String> sportsInfo = savedInstanceState.getStringArrayList("info");
ArrayList<String> sportsDetail = savedInstanceState.getStringArrayList("detail");
ArrayList<Integer> sportsImageResource = savedInstanceState.getIntegerArrayList("img");
//Clear the existing data (to avoid duplication)
mSportsData.clear();
//Create the ArrayList of Sports objects with the titles and information about each sport
for (int i = 0; i < sportsList.size(); i++) {
mSportsData.add(new Sport(sportsList.get(i), sportsInfo.get(i), sportsDetail.get(i), sportsImageResource.get(i)));
}
hasData = true;
}
}
if(!hasData) {
//Get the resources from the XML file
String[] sportsList = getResources().getStringArray(R.array.sports_titles);
String[] sportsInfo = getResources().getStringArray(R.array.sports_info);
String[] sportsDetail = getResources().getStringArray(R.array.sports_detail);
TypedArray sportsImageResource = getResources().obtainTypedArray(R.array.sports_images);
//Clear the existing data (to avoid duplication)
mSportsData.clear();
//Create the ArrayList of Sports objects with the titles and information about each sport
for (int i = 0; i < sportsList.length; i++) {
mSportsData.add(new Sport(sportsList[i], sportsInfo[i], sportsDetail[i], sportsImageResource.getResourceId(i, 0)));
}
sportsImageResource.recycle();
}
//Notify the adapter of the change
mAdapter.notifyDataSetChanged();
}
I'm using SharedPreferences to store a list of fragments of my android, however it gave me thousands lines of error which make non-sense:
2021-12-07 17:09:13.228 14833-14833/? E/AndroidRuntime: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:1058)
at .......
(just small parts of error messages)
I tried to store a list of strings it works fine, but if I switch to an object with list of fragments it fails.
Here is my object.java:
public class CardList {
ArrayList<Card> list; //Card is my fragments
public CardList(ArrayList<Card> list) {
this.list = list;
}
public void updateList(Card c) {
list.add(c);
}
public int getListSize() {
return list.size();
}
}
here is my MainActivity.java, the new intent here was just to test if I can get my data back when I'm back to MainActivity
public class MainActivity extends AppCompatActivity {
public static final String WEATHER_PREFS = "weatherPrefs";
public CardList cardList;
public ArrayList<Card> cards;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = new ArrayList<>();
cardList = new CardList(cards);
SharedPreferences setting = getSharedPreferences(WEATHER_PREFS,MODE_PRIVATE);
Gson gson = new Gson();
String savedList = setting.getString("cardList","");
if (!savedList.equals("")) {
cardList = gson.fromJson(savedList,CardList.class);
}
Button btn= findViewById(R.id.switchActtivity);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Gson gson = new Gson();
String savedInstanceString = gson.toJson(cardList);
SharedPreferences setting = getSharedPreferences(WEATHER_PREFS,MODE_PRIVATE);
SharedPreferences.Editor editor = setting.edit();
editor.putString("cardList",savedInstanceString);
editor.apply();
startActivity(intent);
}
});
Button increase = findViewById(R.id.increase);
increase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cardList.updateList(Card.newInstance("hello","world"));
Log.i("Array List Size After Click",String.valueOf(cardList.getListSize()));
}
});
}
}
Can someone teach me how to store custom object with an arraylist attribute in Android? Thanks in advance!
Fragments aren't serializable, meaning they can't be broken down into pure data ie JSON, much less deserializable.
Looking at your code there is no good way to just make it work either.
You need to follow separation of concerns, Fragments are glorified views. They shouldn't be holding information to any great extent, so they shouldn't be passed around as if they did.
What you could do is have a CardModel that contains all information needed for a Card Fragment.
The CardModel can contain simple data that you would find in JSON like Integer,String, etc...
A List<CardModel> can be deserialized from JSON. And then when you're in the MainActivity you could create as many Card Fragments as needed to represent the List<CardModel> with a simple list.size()
I am working on a Quiz app. First when a user opens the app they go to the MainActivity, from there when they press start they go to the Categories Activity , from there after selecting a category they go to the Sets Activity, from there after selecting a set the go to the Questions Activity and finally after completing all the questions they reach the Score Activity. Here in the score activity when the click on Done button they are redirected to the MainActivity. In the Score Activity i want to change the color of the Set that they completed to green instead of the default color. How can i do this? I created a sets item layout xml file and used an adapter to fill the gridview in the Sets Activity with views from the adapter. Currently i am getting a null object reference after clicking the Done button in the ScoreActivity.
Here is the code :
SetsAdapter.java
public class SetsAdapter extends BaseAdapter {
private int numOfSets;
public SetsAdapter(int numOfSets) {
this.numOfSets = numOfSets;
}
#Override
public int getCount() {
return numOfSets;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view;
if(convertView == null){
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.set_item_layout, parent, false);
}
else {
view = convertView;
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent questionIntent = new Intent(parent.getContext(), QuestionActivity.class);
questionIntent.putExtra("SETNUM", position +1);
parent.getContext().startActivity(questionIntent);
}
});
((TextView) view.findViewById(R.id.setNumber)).setText(String.valueOf(position+1));
return view;
}
}
SetsActivity.java
public class SetsActivity extends AppCompatActivity {
private GridView sets_grid;
private FirebaseFirestore firestore;
public static int categoryID;
private Dialog loadingDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sets);
Toolbar toolbar = (Toolbar)findViewById(R.id.set_toolbar);
setSupportActionBar(toolbar);
String title = getIntent().getStringExtra("CATEGORY");
categoryID = getIntent().getIntExtra("CATEGORY_ID",1);
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
sets_grid = findViewById(R.id.sets_gridView);
loadingDialog = new Dialog(SetsActivity.this);
loadingDialog.setContentView(R.layout.loading_progressbar);
loadingDialog.setCancelable(false);
loadingDialog.getWindow().setBackgroundDrawableResource(R.drawable.progress_background);
loadingDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
loadingDialog.show();
firestore = FirebaseFirestore.getInstance();
loadSets();
}
private void loadSets() {
firestore.collection("Quiz").document("CAT" + String.valueOf(categoryID))
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot doc = task.getResult();
if (doc.exists()) {
long sets = (long) doc.get("SETS");
SetsAdapter adapter = new SetsAdapter(Integer.valueOf((int)sets));
sets_grid.setAdapter(adapter);
} else {
Toast.makeText(SetsActivity.this, "No Sets Exists!", Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(SetsActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
loadingDialog.cancel();
}
});
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
}
ScoreActivity.java
public class ScoreActivity extends AppCompatActivity {
private TextView score;
private Button done;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
score = findViewById(R.id.score_tv);
done = findViewById(R.id.score_activity_done);
String score_str = getIntent().getStringExtra("SCORE");
final int setNum = getIntent().getIntExtra("SetNum", 1);
score.setText(score_str);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here is the issue I am facing
View view = findViewById(R.id.setNumber);
view.setBackgroundColor(Color.GREEN);
Intent mainIntent = new Intent(ScoreActivity.this, MainActivity.class);
startActivity(mainIntent);
ScoreActivity.this.finish();
}
});
}
}
As your activity Sequence is MainActivity -> Categories -> Sets -> Scores.
You've two options to change the color with two different life cycle of the change.
To change the color on a temporary basis, this will reset itself after closing the app or resrtating the 'Sets' activity. It can be done in two ways: Using Public Static Variable and using a public function.
To change the color on a permanent basis until the app is uninstalled/reinstalled. You should use SharedPreferences. SharedPreferences acts like a private data stored in device's memory for further use and it stays there unchanged until and unless the app is removed/data is cleared. Although, apps with root permission can access any app's SharedPreferences data and can modify it as well.You can use SharedPreferences as explained here. Or, you can use some library to access it an easy way. The way I use it in all my apps is TinyDB(it's just a java/kotlin file). This works as:
//store the value from ScoreActivity after completion as
TinyDB tinyDB = TinyDB(this);
tinyDB.putBoolean("isSet1Completed",true);
//access the boolean variable in SetsActivity to change the color of any set that
//is completed and if it's true, just change the color.
TinyDB tinyDB = TinyDB(this);
Boolean bool1 = tinyDB.getBoolean("isSet1Completed");
But, it's your choice what way you want to prefer.
Now, this was about the lifecycle of the change you'll do: Temp or Permanent. Now, we'll talk about how you change the color.
Using public static variable in Sets activity. What you can do is you can set the imageView/textview whose background you want to change as public static variable. Remember, this idea is not preferred as it causes memory leak but it's just easy.
Declare it as public static ImageView imageview;(or TextView) intialize it in the
onCreated() as imageView = finViewById(R.id.viewId); in Sets activity. Call
it as new SetsActivity().imageView.setBackgroundColor(yourColor); in ScoreActivity.
Second way is to create a public function in SetsAcitvity, putting the color change code in it, and then calling it from the ScoreActivity. Just declare it as public void changeColor(){ //your work} and call it from ScoreActivity as new SetsActivity().changeCOlor(). You can also pass some arguments to the function like setId.
I've provided you every thing you need. Rest you should figure out yourself to actually learn it and not copy it.
I think simply you add flag in MainActivity.
for example, add flag in MainActivity.
boolean isFromDone = false;
and when done clicked,
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here is the issue I am facing
Intent mainIntent = new Intent(ScoreActivity.this, MainActivity.class);
mainIntent.putExtra("FromDone", true);
startActivity(mainIntent);
ScoreActivity.this.finish();
}
});
and in MainActivity, add this.
#Override
protected void onResume() {
super.onResume();
isFromDone = getIntent().getBooleanExtra("FromDone", false);
if(isFromDone) {
(TextView) view.findViewById(R.id.setNumber)).setBackgroundColor(Color.GREEN);
}
}
Suppose you have a Linear Layout in Activity A and you want to change it's background color from a button click which is present in Activity B.
Step 1 Create a class and declare a static variable.
class Util { private static LinearLayout mylayout ; }
Step 2
In the activity which is holding this layout, initialize it.
Util.mylayout = findviewbyid(R.id.linear);
Step 3Change the background color on button click from Activity B
onClick{
Util.mylayout.setBackgroundColor(Color.RED);
}
My app doesn't display anything when passing data from one class to another. I located through with the debugger that my ArrayList doesn't get the right value from the class.
I'm sending data with the following function:
public class Adaugare extends AppCompatActivity {
private ListView myListView;
private NumeAdapter numeAdapter;
String inume;
int ivarsta;
Intent intent = new Intent();
private ArrayList persoanaArrayList = new ArrayList<>();
public ArrayList getPersoanaArrayList() {
return persoanaArrayList;
}
public int getPersoanaArrayListsize() {
return persoanaArrayList.size();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adaugare);
myListView = (ListView) findViewById(R.id.persoana_list);
Button btn_fin = (Button) findViewById(R.id.btn_fin);
btn_fin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText nume_edit_text = (EditText) findViewById(R.id.ins_nume);
EditText varsta_edit_text = (EditText) findViewById(R.id.ins_var);
ivarsta = Integer.parseInt(varsta_edit_text.getText().toString());
inume = nume_edit_text.getText().toString();
persoanaArrayList.add(new Persoana(inume, ivarsta));
}
});
}
}
And recieving it with:
public class Afisare extends AppCompatActivity {
ListView myListView;
NumeAdapter numeAdapter;
Adaugare ad = new Adaugare();
int cate;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_afisare);
myListView = (ListView) findViewById(R.id.persoana_list);
ArrayList<Persoana> persoanaArrayList = new ArrayList<Persoana>(ad.getPersoanaArrayList());
numeAdapter = new NumeAdapter(this, persoanaArrayList);
myListView.setAdapter(numeAdapter);
}
The class Persoana is:
public class Persoana {
private String nume;
private int varsta;
Persoana(String inume, int ivar) {
this.nume = inume;
this.varsta = ivar;
}
public String getNume() {
return nume;
}
public int getVarsta() {
return varsta;
}
public void setNume(String nume) {
this.nume = nume;
}
public void setVarsta(int varsta) {
this.varsta = varsta;
}
}
Persoana is the main class, everything is saved in it. ad is an object of Adaugare, Adaugare being the class from which I've taken the code for getPersoanaArrayList. At debugging some values appeared at ad, namely Adaugare #4556, and persoanaArrayList remains null.
I need the persoanaArrayList so that i can initialize my Adapter and listView. Everything else in the code seems fine from step by step testing with debugger.
Your problem is with the following line in the Afisare class:
Adaugare ad = new Adaugare();
You can't simply new one activity from another activity and expect to access a shared list between them. To share instance data between java objects you need a reference to the other object. Creating a new instance will create a new empty list. That's why you are "losing" data. A quick fix would be to make the list static so it can be accessed from any instance.
But since you're dealing with Android, the right way to share data between activities is by using intent extras. The first activity starts the second activity via an intent. The first activity places the desired data in the intent as extras. The second activity uses getIntent() and the various methods on Intent to access the extras.
One last tip, in Android, you never use the new operator with Activities. Activities are created by the system to service an intent. If you find yourself using the new operator, that's a sign that you're doing something wrong.