Sending Variable from Adapter to Activity

Sending variable from Adapter to Activity

When u are inserting, CardID "C" is uppercase while when u are getting it cardID "c" is lowecase.It is case sensitive just make them both the same case and that should work.

How to send a variable to an Adapter to send it through an Intent to another activity?

Firstly, create a MyAdapter constructor where you pass arrayList as well as pedidoId like, your MyAdapter should be something like below:

MyAdapter.class

class MyAdapter(private val platoList : ArrayList<Plato>, val pedidoId:String
) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
........
.....
....

//in your bind(..) method
fun bind(plato: Plato){
platoTouch.setOnClickListener{
intent.putExtra("id", platoName.text.toString())
intent.putExtra("pedidoId", pedidoId)
mActivity.startActivity(intent)
}
}
}

And, in your MenuAtipicoActivity you need to do something like:

MenuAtipicoActivity

class MenuAtipicoActivity : AppCompatActivity() {
...............
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu_atipico)

recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)

platoArrayList = arrayListOf()
pedidoId = intent.extras?.getString("pedidoId") //This is the variable I need to send
myAdapter = MyAdapter(platoArrayList,pedidoId)
recyclerView.adapter = myAdapter
EventChangeListener()

Setup()

}
..........
........

}

How to get the variable value from adapter to activities in android kotlin recyclerview?

Add listerner to you ViewHolder which will notify your adapter and in adapter forward this notification to activity.

override fun populateViewHolder(viewHolder: AddUsersGroupAdapter.ViewHolder?, user : Users?, position: Int, userSelectionListener: (Users, Boolean) -> Unit) {
var userId = getRef(position).key

viewHolder!!.bindView(user!!,context)


viewHolder.itemView.setOnClickListener {
if(viewHolder.userSelected!!){
viewHolder.userSelected = false
//viewHolder!!.bindView(user!!,context)
viewHolder.userCheck!!.visibility = View.GONE
usersSelectionListener.invoke(user, true)

}else{
viewHolder.userSelected = true
viewHolder.userCheck!!.visibility = View.VISIBLE
usersSelectionListener.invoke(user, false)
}

//TODO : I want to collect the Id's into list and
// will access to the activity(AddUserGroupActivity)
//Toast.makeText(context,userId,Toast.LENGTH_LONG).show()

}
}

Add same parameter to your Adapter constructor, then in activity handle this listener:

rvAddUsersGroup.adapter = AddUsersGroupAdapter(mUserDatabase!!, this) { user: User, isSelected: Boolean -> 
//Do want you want with user and selection.
}

How to pass variable from Activity to Adapter

  1. Add argument to your adapter constructor
  2. Pass your variable when instantiating adapter MyGridAdapter adapter = new MyGridAdapter(myVariable)

To update variable from your activity you can also create method in your adapter and call it adapter.updateMyVariable(newVariable);

pass value from adapter to another activity through interface

Your issue with the NullPointerException is due to your variable List<data> mdata; on the activity.

The reason it is crashing is because you've declared variable List<data> mdata; but never assigned value to it, thus it eventually crash due to 'null pointer' because there's no value to read from that variable.

I would suggest following modification on your activity to fix your issue temporarily but still permanent fix would be refactor on OnItemClickListener.

Temporary fix:

On your jsonparse() method, you'll need to assign value to mdata and then use it afterwards like below code snippet:

mdata = response.body().getDataArray(); // remove local variable from here and assign value directly to mdata
mAdapter = new ExampleAdapter(MainActivity.this, mdata); // instead of local variable exampleList, you now pass mdata

This will fix your issue temporarily

Permanent fix:

To fix this properly, I would suggest following refactor (I recommend this approach because it would be less error prone and doesn't require you to have local variable on activity).

Instead of passing position from adapter to activity, you should instead pass variable directly via method, so that you don't end up storing list locally.

Modified interface:

public interface OnItemClickListener {
void onItemClick(data selectedData);
}

& During onClick on adapter item, use it like this: mListener.onItemClick(mExampleList.get(position));

This also changes your code on activity where you override method:

@Override
public void onItemClick(data selectedData) {
Intent detailIntent = new Intent(this, Details.class);
detailIntent.putExtra("email", selectedData.getEmail());
startActivity(detailIntent);
}

Pass value from Activity to adapter

Just add 1 more parameter in your adapter and pass the title

public RoomAdapter(ArrayList<Roompost> bookslist, String title){
this.bookslist = bookslist;
this.title = title;
}

And in your Activity B onCreate

String title = getIntent().getStringExtra("key");
staggeredBooksAdapter = new RoomAdapter(list, title);

Android pass value from activity to adapter

Simply add the values to the constructor.

public SimpleAdapter(Activity context, ArrayList<SimpleBeans> data, String mystring, int myInt){
//use datas here
}

And use it like

myAdapter = new SimpleAdapter(this, data, myString, myInt);

Obiouvsly you can set all the datas you want, mine were some examples.

In your case you simply need to add the arrayList to the constructor.

myAdapter = new SimpleAdapter(this, myArrayList);

How to pass a variable from adapter to fragment?

You should declare a static method in AddNewBoatFragment to create a Fragment instance from given params.

public class AddNewBoatFragment extends Fragment {

private static final String ARGUMENT_BOAT_ID = "ARGUMENT_BOAT_ID";
private static final String ARGUMENT_OWNER_ID = "ARGUMENT_OWNER_ID";

public static AddNewBoatFragment newInstance(int boatId, String ownerId) {
Bundle args = new Bundle();
// Save data here
args.putInt(ARGUMENT_BOAT_ID, boatId);
args.putString(ARGUMENT_OWNER_ID, ownerId);
AddNewBoatFragment fragment = new AddNewBoatFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
// Retrieve the data here
int boatId = getArguments().getInt(ARGUMENT_BOAT_ID);
String ownerId = getArguments().getString(ARGUMENT_OWNER_ID);
return view;
}
}

And modify block code in onClick method from the Adapter class.

holder.boatList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = AddNewBoatFragment.newInstance(product.getId(), product.getOwner_id());
FragmentManager fm = ((AppCompatActivity)mCtx).getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.dahsboard_fragment, fragment);
ft.commit();
}
});


Related Topics



Leave a reply



Submit