-2

I have created an volley list in this i have problem to get data from adapter to activity and this activity to another activity. I have received error cannot cast activity.java to anotherActivity.java below is my code. Please help me anyone thanks.

My Interface itemclick in Adapter class

private OnItemClickGetPlaylist mListener;
public interface OnItemClickGetPlaylist{
    public void OnPlaylistItemClick(String playlistName,int numOfItems,String imageViewForPlaylist);
}
public void setOnClickListenerOnPlaylist(OnItemClickGetPlaylist listener)
{
    mListener = listener;
}
 holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String id = playlist.getId_playlist_identify();
            String PlaylistName = playlist.getTitile_of_playlist();
            String imageOfPlaylist = playlist.getImage_of_playlist();
            int numOfPlaylistSongs = getItemCount();
            SendIdToDatabase(id);
            if (mListener != null)
            {
                mListener.OnPlaylistItemClick(PlaylistName,numOfPlaylistSongs,imageOfPlaylist);
            }
            else {
                Toast.makeText(context, "mListeren is null" + mListener, Toast.LENGTH_SHORT).show();
            }
        }
    });

After get data handle OnPlaylistItemClick click in Activity below Codes

public void OnItemClickHandleInPlaylistActivity(String playlistName,int numOfItems,String imageViewForPlaylist)
{

//here is the adapter item click in activity now i want to send that data to another activity without any intent please help me.

// i have implement below code but it give me cannot cast activity to another activity error.

((anotherActivity) getApplicationContext()).OnItemClickInMusicActivity(playlistName,numOfItems,imageViewForPlaylist);
}
Jared Forth
  • 1,577
  • 6
  • 17
  • 32

4 Answers4

1

See the solution at https://stackoverflow.com/a/47637313/2413303

public class MyApplication extends Application {
    private static MyApplication INSTANCE;

    DataRepository dataRepository; // this is YOUR class

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
        dataRepository = new DataRepository();
    }

    public static MyApplication get() {
        return INSTANCE;
    }
}

The DataRepository should expose LiveData:

public class DataRepository {
    private final MutableLiveData<MyData> data = new MutableLiveData<>();

    public LiveData<MyData> getMyData() {
        return data;
    }

    public void updateText(String text) {
        MyData newData = data.getValue()
                             .toBuilder() // immutable new copy
                             .setText(text)
                             .build();
        data.setValue(newData);
    }
}

Where the Activity subscribes to this:

public class MyActivity extends AppCompatActivity {
    DataRepository dataRepository;

    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication app = (MyApplication)getApplicationContext();
        dataRepository = app.getDataRepository();

        setContentView(R.layout.main_activity);
        textView = findViewById(R.id.textview);

        dataRepository.getMyData().observe(this, new Observer() {
            @Override
            public void onChange(MyObject myObject) {
                textView.setText(myObject.getText());
            }
        }
    }

So to update this text, you need to get the DataRepository class, and call updateText on it:

DataRepository dataRepository = MyApplication.get().dataRepository();
dataRepository.updateText("my new text");

Make sure that the data in DataRepository is properly persisted somewhere, or at least can be obtained again after process death. You might want to use a database for example (but not shared preferences).

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

I find the best to use callbacks.

in ClassA: Create interface

MyCallback callback;

viod setCallback(MyCallback callback){
this.callback = callback;
}
viod onStop(){
    callback = null;
}

interface MyCallback{
    void doSomething(Params params);
}

in ClassB: implement MyCallback

public class ClassB **implements ClassA.MyCallback**

set reference in onCreate

ClassA classA = new ClassA();
classA.setCallback(this);

// override method doSomething

@override
void doSomething(Params params){
 //do your thing with the params…
}

when the job is done inside class A call:

callback.doSomething(params);

destroy reference inside class B in onStop()

classA.onStop();
MarkWalczak
  • 1,532
  • 3
  • 16
  • 24
0

If you don't want to use Intents, maybe you can use a publish/subscribe architecture. There is a library called eventbus (org.greenrobot:eventbus) very easy to use which could achieve what you want.

AlbertMun
  • 1
  • 2
0

Use an Application Class instead.

public class MyApplicationClass extends Application{
       @Override
       public void onCreate() {
          super.onCreate();
          ///do something on create
    }
       getterMethods(){...}
       setterMethods(){...}
}

then add android:name=".MyApplicationClass" to manifest file

Now you can access the methods in class by

MyApplicationClass applicationClass = (MyApplicationClass)getApplicationContext();
int id = applicationClass .getterMethod().getID;
String playListName = applicationClass .getterMethod().getPlayListName();

and vice versa for Setter(); after that you can use it for data getting and setting Data throughout the application.

Hope it helps :)

References: https://guides.codepath.com/android/Understanding-the-Android-Application-Class

Ali
  • 519
  • 4
  • 13