0

So im getting my data from a web service and using RecyclerView adapter.In RecyclerView.adapter's onBindviewholder method I want to pass the data to the recyclerView in the MainActivity but also pass the data (item) to MyMapsActivity.The thing is the onBindViewholder has setOnClickListener and setOnLongClickListener as controls that are being used. Is there either a way to send the data(item) using an intent that doesnt start the activity or is there a way I can wire up a new button within the onBindViewholder method because what happens is when the app starts up it goes straight to MyMapsActivity which is expected. Is there a way to control this by either using a new button that can interact with onBindViewholder or is there a way to pass the intent.putExtra without starting the MyMapsActivity.Maybe My understanding is flawed but here is my code for the methods and activities stated:

onBindViewholder

public void onBindViewHolder(DataItemAdapter.ViewHolder holder, int position) {
        final DataItem item = mItems.get(position);


    try {
        holder.titletext.setText(item.getTitle());
        holder.companyText.setText(item.getCompany());
        holder.cityText.setText(item.getCity());
        holder.salarytext.setText(""+ item.getSalary());
        holder.descriptionText.setText(item.getDescription());
        holder.responsibilityText.setText(item.getResponsibility());
        holder.latText.setText(""+ item.getLatitude());
        holder.lngText.setText(""+ item.getLongitude());
        holder.phoneText.setText(item.getPhone());
        holder.provinceText.setText(item.getProvince());
    } catch (Exception e) {
        e.printStackTrace();
    }
    //click
    holder.mView.setOnClickListener(new View.OnClickListener() {//getting viewholder class and ctor
        @Override
        public void onClick(View v) {


            Intent intent = new Intent(mContext,DetailsActivity.class);
            intent.putExtra(ITEM_KEY,item);
           mContext.startActivity(intent);
        }
    });
    //long click
    holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(mContext, "long click: " + item.getTitle(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

        // !!!!!----this is the intent Im talking about----!!!
        Intent intent = new Intent(mContext,MyMapActivity.class);
        intent.putExtra(ITEM_KEY,item);
         mContext.startActivity(intent);

}

My maps method :

    public void onMapReady(GoogleMap googleMap) {
            final DataItem item = getIntent().getExtras()
.getParcelable(DataItemAdapter.ITEM_KEY); //--------gets intent frm dataItemAdapter

    if (item == null) {
        throw new AssertionError("null data recived");
    }
    mapReady = true;
    mMap = googleMap;
    LatLng latLngToronto = new LatLng(43.733092, -79.264254);
   // LatLng latLnghome = new LatLng(43.656729, -79.377162);

    CameraPosition target = CameraPosition.builder().target(latLngToronto).zoom(15).tilt(65).build();
    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(target));
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);


    markerDisplay(item.getTitle(),item.getLatitude(),item.getLongitude());//------maps jobs marker-------new


    //add markers and
    //instantiate
   //   mMap.addMarker(mp);
  //  mMap.addMarker(md);


} 
Mohamoud Mohamed
  • 515
  • 7
  • 16
  • If you need to pass data between activities, check this [link](https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities#answer-4878259) – matoni Aug 12 '17 at 21:09
  • Thanks my issue was persisting the data I used an Arraylist and iterated using a foreach. I was over thinking it. – Mohamoud Mohamed Aug 13 '17 at 17:29

1 Answers1

0

You do not need to call startAcivity method. There is sendBroadcast method for sharing intent to other components.

 String CUSTOM_ACTION = "com.example.YOUR_ACTION";

 Intent i = new Intent();
 i.setAction(CUSTOM_ACTION)
 intent.putExtra(ITEM_KEY,item);
 context.sendBroadcast(intent);

Then you can receive your intent with BroadcastReceiver

public class MyReciever extends BroadcastReceiver {
@Override
    public void onReceive(Context context, Intent intent) { 
        String action = intent.getAction();
        if(action!=null && action.equals(CUSTOM_ACTION)){
           //do something 
        }
    }
}

register receiver with custom IntentFilter into onCreate method of your activity

IntentFilter filter = new IntentFilter(CUSTOM_ACTION);
registerReceiver(myReceiver, filter);

And do not forget unregister it in onDestroy

unregisterReceiver(myReceiver);

P.S

There are many ways of transferring data between classes and components. It depends on your purpose.
Broadcast receiver with intents are good for data transfer between services and activities and for data transfer between apps.

If you need to have access to data from different places then there are other ways.
If the data needs to be stored on a disk, then the database is suitable.
To store data in RAM, you can use separate class and store this class as a Singleton or into your Application class.

Dmitriy Puchkov
  • 1,530
  • 17
  • 41
  • hey trying to work on it getting crashes when I try and load up the Map activity – Mohamoud Mohamed Aug 12 '17 at 22:29
  • How do you get data from the broadcast reciver and share it in the activity? Lets say you get the daya and it is in the if(action.... how do you share that data with the rest of the activity – Mohamoud Mohamed Aug 12 '17 at 22:45
  • You can create inner class extends broadcast receiver into your activity. Inner classes have access to fields and methods of outer class. – Dmitriy Puchkov Aug 13 '17 at 07:29
  • Do you mean an inner class in the Activity I did that. Do I have to register the receiver in the manifest or the oncreate. The thing is that I am implementing onReadyMap and I need to pass the data there aswell.So If Im using a LocalBroadcastManager to send the intent now.But I feel like when Im debugging the whole broadcast reciver class is skipped by the program. – Mohamoud Mohamed Aug 13 '17 at 15:26
  • Hey I solved the issue I actually didnt need a intent or anything I just persisted the data in a ArrayList and iterated through it it worked.Thanks for your input . My issue was persisting the data im new and need to know about more storage types. – Mohamoud Mohamed Aug 13 '17 at 17:27
  • I said answered because you actually answered the question I was asking. But I took a different approach. – Mohamoud Mohamed Aug 13 '17 at 18:14
  • @fasecity I'm glad that you found a solution. Thank you, good luck. – Dmitriy Puchkov Aug 13 '17 at 18:21
  • Can I ask you something whats a good time to use itnent.putextra/broadcast receivers instead of using lists for your app? – Mohamoud Mohamed Aug 13 '17 at 18:24