我有一个片段,其左右两侧有2个recyclerview
。如何将数据从第一个recyclerview
发送到第二个recyclerview
并直接更新其数据?
我在活动中取得了与下面的脚本一样成功的结果,但是现在我感到困惑,如果放在片段中。
第一个recyclerview
是食物菜单列表,第二个recyclerview
是已订购的食物菜单。
第一个recyclerview
脚本“ AdapterMenu”,即单击菜单时的脚本。
holder.llmenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
DataMenu menu = menuModels.get(position);
Toast.makeText(getActivity(), "You clicked at position: " + (menu.getNama_menu()), Toast.LENGTH_SHORT).show();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AddMenu)
.addConverterFactory(GsonConverterFactory.create())
.build();
API api = retrofit.create(API.class);
Call<DataTambahValue> call = api.tambah(id,menu.getMenu_id());
call.enqueue(new Callback<DataTambahValue>() {
@Override
public void onResponse(Call<DataTambahValue> call, Response<DataTambahValue> response) {
String status = response.body().getStatus();
String message = response.body().getMessage();
if (status.equals("1")) {
// sukses, refresh data yang nampilin daftar pesanan.
Alerter.create(activity)
.setTitle("Informasi")
.setBackgroundColor(R.color.alerterSuccess)
.setText("Pesanan Ditambahkan ")
.show();
if (activity instanceof MainActivity){
MainActivity mainActivity = (MainActivity) activity;
mainActivity.loadPesanan();
}
Log.w("Status",message);
} else {
Alerter.create(activity)
.setTitle("Informasi")
.setBackgroundColor(R.color.alerterError)
.setText("Jaringan Error")
.show();
// gagal.
Log.w("Status",message);
}
}
@Override
public void onFailure(Call<DataTambahValue> call, Throwable t) {
}
});
}
});
}
脚本recyclerview第二个“ AdapterMenuMesan”,设置脚本时的订单数。
holder.pemesananTambah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {String idMenu = item.getPemesananId();
Integer qty = Integer.parseInt(String.valueOf(holder.pemesananJumlah.getText()));
qty++;
// panggil retrofit untuk update qty nya.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
//menyambungkan dengan webservice API edit
API api = retrofit.create(API.class);
Call<ResponseValue> call = api.edit("edit",idMenu, String.valueOf(qty));
call.enqueue(new Callback<ResponseValue>() {
@Override
public void onResponse(Call<ResponseValue> call, Response<ResponseValue> response) {
String status = response.body().getStatus();
String message = response.body().getMessage();
if (status.equals("1")) {
// sukses, refresh data yang nampilin daftar pesanan.
Activity act = (Activity) activity;
if (act instanceof MainActivity) { // reload data pesanan
MainActivity main = (MainActivity) act;
main.loadPesanan();
Alerter.create(act)
.setTitle("Informasi")
.setBackgroundColor(R.color.alerterSuccess)
.setText("Jumlah Pesanan " + item.getNama_menu() + " Ditambah")
.show();
}
} else {
Activity act = (Activity) activity;
if (act instanceof MainActivity) { // reload data pesanan
MainActivity main = (MainActivity) act;
main.loadPesanan();
Alerter.create(act)
.setTitle("Informasi")
.setBackgroundColor(R.color.alerterError)
.setText("Jumlah Pesanan " + item.getNama_menu() + " Gagal Ditambah ")
.show();
}
}
}
@Override
public void onFailure(Call<ResponseValue> call, Throwable t) {
}
});
}
});
holder.pemesananKurang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {Integer qty = Integer.parseInt(String.valueOf (holder.pemesananJumlah.getText()));
qty--;
String idMenu = item.getPemesananId();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
//menyambungkan dengan webservice API
API api = retrofit.create(API.class);
Call<ResponseValue> call = api.edit("edit",idMenu, String.valueOf(qty));
call.enqueue(new Callback<ResponseValue>() {
@Override
public void onResponse(Call<ResponseValue> call, Response<ResponseValue> response) {
String status = response.body().getStatus();
String message = response.body().getMessage();
if (status.equals("1")) {
// sukses, refresh data yang nampilin daftar pesanan.
Activity act = (Activity) activity;
if (act instanceof MainActivity) { // reload data pesanan
MainActivity main = (MainActivity) act;
main.loadPesanan();
Alerter.create(act)
.setTitle("Informasi")
.setBackgroundColor(R.color.alerterSuccess)
.setText("Jumlah Pesanan " + item.getNama_menu() + " Ditambah")
.show();
}
} else {
Activity act = (Activity) activity;
if (act instanceof MainActivity) { // reload data pesanan
MainActivity main = (MainActivity) act;
main.loadPesanan();
Alerter.create(act)
.setTitle("Informasi")
.setBackgroundColor(R.color.alerterError)
.setText("Jumlah Pesanan " + item.getNama_menu() + " Gagal Ditambah ")
.show();
}
}
}
@Override
public void onFailure(Call<ResponseValue> call, Throwable t) {
}
});
}
});
}
此方法loadMenu()和load Order()。片段_1中的脚本
public class Fragment_1 extends Fragment {
//private RecyclerView recyclerView;
private StaggeredGridLayoutManager staggeredGridLayoutManager;
public AdapterMenu mAdapter;
public AdapterMenuPesanan mpAdapter;
private List<DataMenu> menuModels = new ArrayList<>();
private List<DataItem> itemModels = new ArrayList<>();
private Context context;
public static final String PREF = "ID";
public static final String URL ="http://192.168.99.98/webserviceKoffie/api/";
SharedPreferences sharedPreferences;
@BindView(R.id.recyclerview_menu) RecyclerView recyclerView;
@BindView(R.id.recyclerview_pesanan) RecyclerView recyclerViewPesan;
@BindView(R.id.btnPesanMenu) Button btnpesan;
@BindView(R.id.Total) Button Total;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_1, container, false);
getActivity().setTitle(R.string.app_name);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_menu);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new AdapterMenu(menuModels,getActivity()));
itemModels.clear();
recyclerViewPesan = (RecyclerView) view.findViewById(R.id.recyclerview_pesanan);
recyclerViewPesan.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager1 = new LinearLayoutManager(getActivity());
recyclerViewPesan.setLayoutManager(layoutManager1);
recyclerViewPesan.setAdapter(new AdapterMenuPesanan(itemModels,getActivity()));
loadMenu();
loadPesanan();
return view;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
private void finish() {
}
public void loadMenu(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
API api = retrofit.create(API.class);
Call<DataMenuValue> call = api.menu();
call.enqueue(new Callback<DataMenuValue>() {
@Override
public void onResponse(Call<DataMenuValue> call, Response<DataMenuValue> response) {
String value = response.body().getStatus();
menuModels = response.body().getData();
if(value.equals("1")){
mAdapter = new AdapterMenu(menuModels,getActivity());
Log.d(String.valueOf(response.body().getData()), "onResponse: aaaa");
staggeredGridLayoutManager = new StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.VERTICAL);
staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(mAdapter);
SpaceItemDecorationView decoration = new SpaceItemDecorationView(getActivity(),5);
recyclerView.addItemDecoration(decoration);
}
}
@Override
public void onFailure(Call<DataMenuValue> call, Throwable t) {
}
});
}
public void loadPesanan(){
SharedPreferences pref = getActivity().getSharedPreferences(PREF, getActivity().MODE_PRIVATE);
final String id = pref.getString("id",null);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
API api = retrofit.create(API.class);
Call<DaftarPesananValue> call = api.daftarPesanan("getData",id);
call.enqueue(new Callback<DaftarPesananValue>() {
@Override
public void onResponse(Call<DaftarPesananValue> call, Response<DaftarPesananValue> response) {
int value = response.body().getStatus();
itemModels = response.body().getDatas();
int harga = 0;
for(int i=0; i<itemModels.size();i++){
harga += Integer.parseInt(itemModels.get(i).getTotalHarga());
}
Log.w("Total",String.valueOf(harga));
Total.setText(String.valueOf(harga));
if(value == 1){
mAdapter = new AdapterMenu(menuModels,getActivity());
RecyclerView.LayoutManager mLayoutManagerPemesanan = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManagerPemesanan);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
else{
Total.setText("0");
mpAdapter = new AdapterMenuPesanan(itemModels, getActivity());
RecyclerView.LayoutManager mLayoutManagerPemesanan = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManagerPemesanan);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mpAdapter);
}
}
@Override
public void onFailure(Call<DaftarPesananValue> call, Throwable t) {
}
});
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
我知道
中的错误Activity act = (Activity) activity;
if (act instanceof MainActivity) {// reload order data
MainActivity main = (MainActivity) act;
main.loadOrder ();
我很困惑在片段中替换MainActivity的方法,我已经用fragment_1替换了,但是仍然出错。我该怎么办?