Reputation: 810
I need to send the detail when I click on an item in a RecyclerView to another Activity..
// Here I call my service
public void getContactoById(Integer id)
{
apiManager.getContactoById(id).
enqueue(new Callback<Contacto>() {
@Override
public void onResponse(Call<Contacto> call, Response<Contacto> response) {
if (response.isSuccessful()) {
// ???????? How send from here to 2nd Activity
}
}
@Override
public void onFailure(Call<Contacto> call, Throwable t) {
Throwable x = t;
}
});
}
// In second Activity, I need to fill these controls with the values obtained from the service call
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_contact);
Service.getInstance().getContactoById(Integer.valueOf(contacto.getUserId()));
imgFoto = (ImageView) findViewById(R.id.imgFoto);
tvNombre = (TextView) findViewById(R.id.tvNombre);
tvPhoneHome = (TextView) findViewById(R.id.tvPhoneHome);
tvPhoneCellphone = (TextView) findViewById(R.id.tvPhoneCellphone);
tvPhoneOffice = (TextView) findViewById(R.id.tvPhoneOffice);
tvBirthDate = (TextView) findViewById(R.id.tvBirthDate);
}
I have to do it calling with Retrofit but the values remain empty because the Response arrives seconds after the start of the second Activity. Can the controls be updated from the Call Response? Thank you.
Upvotes: 0
Views: 3420
Reputation: 602
The best method is to pass the data through an interface. Since your response is in asynchronous task, we need to pass it through an interface.
This interface can be implemented in activity where data [ your api response ] is required.
Step 1: you need create interface and define function
public interface onAddTextViewCustomListener {
void onAddText(String text);
}
Step 2: use this interface in 'onResponse' of your retrofit call
//declare interface object
onAddTextViewCustomListener onAddTextViewCustomListener; //listener custom
//add contructor
this.onAddTextViewCustomListener = onAddTextViewCustomListener;
//call interface and pass data
onAddTextViewCustomListener.onAddText("your api response");
Step 3: Implements the interface to activity like this:
public class MainActivity extends AppCompatActivity implements onAddTextViewCustomListener
You will get the method defined inside interface in that activity after implementing it.
public void onAddText(String text) {
Log.i("API RESPONSE", text);
// execute your logic here. }
Upvotes: 3
Reputation: 423
Create an array list and by the time, you fetch data from api, add it to an Arraylist. After that in your adapter, use data from that arraylist to send data from one activity to another activity.
tv_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext,WebviewActivity.class);
i.putExtra(INTENT_CHANNELURL,al_details.get(getLayoutPosition()).get(URL_WEB).toString ());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(i);
}
});
In my case, i created an Arraylist named al_details.
Upvotes: 0
Reputation: 1255
Use Bundle
In Activity1, pass data:
Intent intent = new Intent(this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putString("key1",value1);
bundle.putInt("key2",value2);
...
intent.putExtras(bundle)
startActivity(intent);
In Activity2, retrieve data:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value1 = extras.getString("key1");
int value2 = extras.getInt("key2");
...
}
Upvotes: 0
Reputation: 18
other way you may use something like
Intent intX=new Intent(this, otherActivity.class);
intX.putExtra("stringextra1","value extra1");
intX.putExtra("stringextra2","value extra2");
intX.putExtra("stringextra_x","value extra_x");
startActivityForResult(intX, 0);
and on otherActivity class receive that extra string
Upvotes: 0
Reputation: 122
it's my last project and I wish to help you:
create an adapter for your recyclerview, like below:
public class ListAddressAdapter extends RecyclerView.Adapter<ListAddressAdapter.MyViewHolder> {
Context context;
List<ShippingInfoResponse> list;
ApiInterface apiInterface;
SelectAddress addressClicked;
deleteAddress deleteClicked;
boolean cardViewClicked = false;
public ListAddressAdapter(Context context, List<ShippingInfoResponse> list) {
this.context = context;
this.list = list;
}
@Override
public ListAddressAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adress_list_item, parent, false);
return new ListAddressAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ListAddressAdapter.MyViewHolder holder, final int position) {
holder.cardView.setBackgroundColor(context.getResources().getColor(R.color.white));
holder.txtShowName.setText(list.get(position).getName());
holder.txtShowProvince.setText(list.get(position).getProvince());
holder.txtShowCity.setText(list.get(position).getCity());
holder.txtShowAddress.setText(list.get(position).getCity()+" "+list.get(position).getAddress());
holder.txtShowNumber.setText(list.get(position).getMobile());
holder.txtEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, AddAddressActivity.class);
intent.putExtra("id", list.get(position).getId());
intent.putExtra("name", list.get(position).getName());
intent.putExtra("province", list.get(position).getProvince());
intent.putExtra("city", list.get(position).getCity());
intent.putExtra("address", list.get(position).getAddress());
intent.putExtra("postalCode", list.get(position).getPostalCode());
intent.putExtra("mobile", list.get(position).getMobile());
context.startActivity(intent);
}
});
holder.txtSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addressClicked.selectAddress(
list.get(position).getName(),
list.get(position).getAddress(),
list.get(position).getMobile(),
list.get(position).getId(),
list.get(position).toString()
);
Log.e("select", "clicked");
}
});
holder.txtDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteClicked.deleteAddress(list.get(position).getId());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtShowName;
TextView txtProvince, txtShowProvince;
TextView txtCity, txtShowCity;
TextView txtAddress, txtShowAddress;
TextView txtNumber, txtShowNumber;
TextView txtDelete;
TextView txtEdit;
CardView cardView;
LinearLayout linearTxtEdit;
TextView txtSelect;
public MyViewHolder(View itemView) {
super(itemView);
txtName = (TextView) itemView.findViewById(R.id.txt_name_in_list_address);
txtShowName = (TextView) itemView.findViewById(R.id.txt_show_name);
txtProvince = (TextView) itemView.findViewById(R.id.txt_province_in_list_address);
txtShowProvince = (TextView) itemView.findViewById(R.id.txt_show_province);
txtCity = (TextView) itemView.findViewById(R.id.txt_city_in_list_address);
txtShowCity = (TextView) itemView.findViewById(R.id.txt_show_city);
txtAddress = (TextView) itemView.findViewById(R.id.txt_address_in_list_address);
txtShowAddress = (TextView) itemView.findViewById(R.id.txt_show_address);
txtNumber = (TextView) itemView.findViewById(R.id.txt_number_in_list_address);
txtShowNumber = (TextView) itemView.findViewById(R.id.txt_show_number);
txtEdit = (TextView) itemView.findViewById(R.id.txt_edit);
cardView = (CardView) itemView.findViewById(R.id.cardItem_address);
linearTxtEdit = (LinearLayout) itemView.findViewById(R.id.linearTxtEdit);
txtSelect = (TextView) itemView.findViewById(R.id.txt_select_address);
txtDelete = (TextView) itemView.findViewById(R.id.txt_delete_Address);
txtEdit.setTypeface(Application.vazirDigit);
txtName.setTypeface(Application.vazirDigit);
txtShowName.setTypeface(Application.vazirDigit);
txtProvince.setTypeface(Application.vazirDigit);
txtShowProvince.setTypeface(Application.vazirDigit);
txtCity.setTypeface(Application.vazirDigit);
txtShowCity.setTypeface(Application.vazirDigit);
txtAddress.setTypeface(Application.vazirDigit);
txtShowAddress.setTypeface(Application.vazirDigit);
txtNumber.setTypeface(Application.vazirDigit);
txtShowNumber.setTypeface(Application.vazirDigit);
txtSelect.setTypeface(Application.vazirDigit);
txtDelete.setTypeface(Application.vazirDigit);
}
}
public interface SelectAddress {
public void selectAddress(String name, String address, String number, int id, String position);
}
public void setOnSelectAddressClicked(SelectAddress selectAddressClicked) {
this.addressClicked = selectAddressClicked;
}
public interface deleteAddress {
public void deleteAddress(int id);
}
public void setOnDeleteClicked(deleteAddress deleteAddressClicked) {
this.deleteClicked = deleteAddressClicked;
}
}
and in your Activity :
private void fillRecyclerView() {
adapter = new ListAddressAdapter(this, info);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
avl.setVisibility(View.GONE);
adapter.setOnSelectAddressClicked(new ListAddressAdapter.SelectAddress() {
@Override
public void selectAddress(String name, String address, String number, int id, String position) {
Name = name;
Address = address;
Number = number;
Id = id;
deleteId=id;
Log.e("Name", name);
SelectAddressFromList();
}
});
adapter.setOnDeleteClicked(new ListAddressAdapter.deleteAddress() {
@Override
public void deleteAddress(int id) {
deleteId = id;
Log.w("id", deleteId + "");
deleteLastAddress();
avl.setVisibility(View.GONE);
}
});
}
private void deleteLastAddress() {
apiInterface = ApiClient.getClient(this).create(ApiInterface.class);
Call<Void> call = apiInterface.deleteAddress(deleteId);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Log.w("responceCodeDelete", response.code() + "");
Log.w("responceMessage", response.message());
info.clear();
avl.setVisibility(View.GONE);
getAllAddress();
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
}
});
}
private void SelectAddressFromList() {
apiInterface = ApiClient.getClient(this).create(ApiInterface.class);
Call<Void> call = apiInterface.sendShippingInfo(
Name,
"خوزستان"
, "اهواز",
Address,
"4947118914",
Number
);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Log.w("RspncCodSendAddressFrmLst", response.code() + "");
if (response.isSuccessful()) {
Intent intent = new Intent();
intent.putExtra("Name", Name);
intent.putExtra("Address", Address);
intent.putExtra("id", Id);
Log.w("sendingId", Id + "");
setResult(RESULT_OK, intent);
deleteLastAddress();
finish();
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
}
});
}
Upvotes: 1