0

i'm doing a simple crud android project i'm absolute beginner so i watched a tuto on youtub

i found that he passed un object to intent.putExtra

like this

  Employee emp = e==null? list.get(position):e;
 case R.id.menu_edit:
                        Intent intent=new Intent(context,MainActivity.class);
                        intent.putExtra("EDIT",emp);
                        context.startActivity(intent);
                        break;

code source from here https://github.com/Sovary/FireTestRecyclerView/blob/main/app/src/main/java/com/hellokh/sovary/firetest/RVAdapter.java#L56

and it dosn't work with me ! intent.putExtra can't hold an object in it in my code it only support string or something what to do to pass this argument ?

i tried to change it to char or to give one attribut but no hope hh

Eya Rais
  • 3
  • 3

1 Answers1

0

library:

implementation 'com.google.code.gson:gson:2.10'

Convert your object data to string and send your data as string through intent:

 case R.id.menu_edit:
                        Intent intent = new Intent(context,MainActivity.class);
                        
                        Gson gson = new Gson();
                        //transform a java object to json
                        String employee_string = gson.toJson(Employee.class);
                        
                        intent.putExtra("EDIT",employee_string);
                        context.startActivity(intent);
                        break;

On the destination page get convert the string to object through gson and retrive your data:

String data = getIntent().getStringExtra("EDIT");
//Transform a json to java object
Employee employee= gson.fromJson(json_ data, Employee.class); // here is your data as object