我只是创建一个带有循环视图的对话框。然后,如何从选定的循环视图中获取值并将其设置为相同的活动
上的editextthis is the dialog and the value from recycle view
and this is the value will be set
我调用对话框的代码,并为回收视图设置addOnItemTouchListener。当我运行它时,我的应用程序会强行关闭
@SuppressLint("ResourceType")
@OnClick(R.id.button_choose)
void chooseLOV() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.lov_kodepos, null);
dialogBuilder.setView(dialogView);
final RecyclerView recyclerView = (RecyclerView) dialogView.findViewById(R.id.rv_lov_kodepos);
API.getKodePos().enqueue(new Callback<ArrayList<KodePos>>() {
@Override
public void onResponse(Call<ArrayList<KodePos>> call, Response<ArrayList<KodePos>> response) {
if (response.code()== 200){
Log.i("bella", "onResponse: "+response);
data = response.body();
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(AddCustomerActivity.this, DividerItemDecoration.VERTICAL));
recyclerView.setLayoutManager(new LinearLayoutManager(AddCustomerActivity.this));
recyclerView.setAdapter(new KodePosAdapter(data));
}
}
@Override
public void onFailure(Call<ArrayList<KodePos>> call, Throwable t) {
Toast.makeText(AddCustomerActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(AddCustomerActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(AddCustomerActivity.this,AddCustomerActivity.class);
intent.putExtra(DATA_KODEPOS, tempDatas.get(position).getPoscodeId());
startActivity(intent);
if(dataKodePos.getPoscodeId()==DATA_KODEPOS){
API.setKodePos(getIntent().getStringExtra(DATA_KODEPOS)).enqueue(new Callback<KodePos>() {
@Override
public void onResponse(Call<KodePos> call, Response<KodePos> response) {
KodePos hehe = response.body();
et_provinsi.setText(hehe.getPosProp());
et_kota.setText(hehe.getPosKota());
et_kecamatan.setText(hehe.getPosCamat());
et_kelurahan.setText(hehe.getPosLurah());
et_kodepos.setText(hehe.getPosKode());
}
@Override
public void onFailure(Call<KodePos> call, Throwable t) {
}
});
}else {
}
}
}));
我的适配器
public class KodePosAdapter extends
RecyclerView.Adapter<KodePosAdapter.KodePosViewHolder> {
ArrayList<KodePos> datasSet;
public KodePosAdapter(ArrayList<KodePos> data) {
this.datasSet = data;
}
@Override
public KodePosViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.lov_recyclerview, parent, false);
return new KodePosViewHolder(itemView);
}
@Override
public void onBindViewHolder(KodePosViewHolder holder, int position) {
KodePos kdModel = datasSet.get(position);
holder.kodePos.setText(kdModel.getPosKode());
holder.kecamatan.setText(kdModel.getPosCamat().toLowerCase());
holder.kelurahan.setText(kdModel.getPosLurah().toLowerCase());
}
@Override
public int getItemCount() {
return datasSet.size();
}
public class KodePosViewHolder extends RecyclerView.ViewHolder {
public TextView kodePos, kecamatan, kelurahan;
public KodePosViewHolder(View itemView) {
super(itemView);
kodePos = (TextView)itemView.findViewById(R.id.tv_lov_kodepos);
kecamatan = (TextView)itemView.findViewById(R.id.tv_lov_kecamatan);
kelurahan = (TextView)itemView.findViewById(R.id.tv_lov_kelurahan);
}
}
}
答案 0 :(得分:0)
首先,您需要创建 OnItemClickListener 并将其设置为您的recyclerView适配器,以获取所点击的项目数据。
其次,在对话框中创建一个类似的 OnRecyclerItemClickListener 。在这种情况下,您需要创建一个扩展AlertDiaglog的 CustomAlertDialog 类。
答案 1 :(得分:0)
首先,使用OnClickListener
public class KodePosViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
覆盖onClick
并获取项目的位置
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
}
从此adapterPosition
获取kdModel
然后获取文本并在“警告”对话框中显示。
KodePos kdModel = datasSet.get(adapterPosition);
答案 2 :(得分:0)
我刚刚解决了我的问题。这比我想象的要简单得多。点击项目后,我只需要用这种方式在编辑文本中显示值
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(AddCustomerActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
et_kodepos.setText(data.get(position).getPosKode());
et_kota.setText(data.get(position).getPosKota());
et_provinsi.setText(data.get(position).getPosProp());
et_kecamatan.setText(data.get(position).getPosCamat());
et_kelurahan.setText(data.get(position).getPosLurah());
}
}));