当我在RecyclerView中单击某个项目时,我需要将详细信息发送到另一个活动。
// 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);
}
我必须使用Retrofit进行调用,但是值仍然为空,因为Response在第二个Activity开始后数秒到达。可以从“呼叫响应”中更新控件吗?谢谢。
答案 0 :(得分:2)
最好的方法是通过界面传递数据。 由于您的响应属于异步任务,因此我们需要通过界面传递它。
此接口可以在需要数据[您的api响应]的活动中实现。
步骤1:您需要创建界面并定义函数
public interface onAddTextViewCustomListener {
void onAddText(String text);
}
第2步:在改造调用的“ onResponse”中使用此界面
//declare interface object
onAddTextViewCustomListener onAddTextViewCustomListener; //listener custom
//add contructor
this.onAddTextViewCustomListener = onAddTextViewCustomListener;
//call interface and pass data
onAddTextViewCustomListener.onAddText("your api response");
步骤3: 实现活动接口,如下所示:
public class MainActivity extends AppCompatActivity implements onAddTextViewCustomListener
实现该方法后,您将在该活动中获取接口内部定义的方法。
public void onAddText(String text) {
Log.i("API RESPONSE", text);
//在这里执行您的逻辑。 }
答案 1 :(得分:1)
这是我的最后一个项目,希望对您有所帮助
为您的recyclerview创建适配器,如下所示:
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;
}
}
以及您的活动中:
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) {
}
});
}
答案 2 :(得分:0)
您可以使用
之类的其他方式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);
和otherActivity类接收该额外的字符串
答案 3 :(得分:0)
使用捆绑包
在Activity1中,传递数据:
Intent intent = new Intent(this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putString("key1",value1);
bundle.putInt("key2",value2);
...
intent.putExtras(bundle)
startActivity(intent);
在Activity2中,检索数据:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value1 = extras.getString("key1");
int value2 = extras.getInt("key2");
...
}
答案 4 :(得分:0)
创建一个数组列表,然后您从api中获取数据,并将其添加到Arraylist中。 之后,在适配器中,使用该阵列列表中的数据将数据从一个活动发送到另一活动。
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);
}
});
就我而言,我创建了一个名为al_details的Arraylist。