[英]how can i get data from adapter and show in Activity in Android
在我的应用程序中,我想在dialog
显示国家 在我的应用程序有一些editTexts
在mainActivity
,当点击Contry editText
显示countryDialog并在此对话框中(我得到这个国家从服务器)进行排序的国家。
我想点击县名,在editText
上设置这个国家。
我的适配器代码:
public class CountryAdapter extends RecyclerView.Adapter {
private List<CountryDatum> mData;
private Context context;
public CountryAdapter(List<CountryDatum> mData, Context context) {
this.mData = mData;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country, parent, false);
vh = new DataViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof DataViewHolder) {
((DataViewHolder) holder).countryListTxt.setText(mData.get(position).getName() + "");
((DataViewHolder) holder).countryListTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "" + mData.get(position).getId(), Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public int getItemCount() {
return mData.size();
}
public void add(List<CountryDatum> models) {
mData.addAll(models);
notifyDataSetChanged();
}
public void clear() {
mData.clear();
notifyDataSetChanged();
}
public class DataViewHolder extends RecyclerView.ViewHolder {
private TextView countryListTxt;
public DataViewHolder(View itemView) {
super(itemView);
countryListTxt = (TextView) itemView.findViewById(R.id.countryNameTxt);
}
}
}
主要活动代码:
public class RegisterActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
private String countryName = "";
@BindView(R.id.registerCountryEdtTxt)
EditText countryListEdt;
@BindView(R.id.registerDateBirthEdtTxt)
EditText birthDayEdt;
private CountryAdapter mAdapter;
private List<CountryDatum> models = new ArrayList<>();
private Context context;
private Dialog dialog;
private RecyclerView countryRecyler;
private ProgressBar countryProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Initialize
ButterKnife.bind(this);
context = RegisterActivity.this;
mAdapter = new CountryAdapter(models, context);
}
@OnClick({R.id.registerCountryEdtTxt, R.id.registerCountryInptLay})
void selectCountry() {
getData();
}
@OnClick({R.id.registerDateBirthInptLay, R.id.registerDateBirthEdtTxt})
void selectBirthDay() {
Calendar now = Calendar.getInstance();
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
RegisterActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
datePickerDialog.setVersion(DatePickerDialog.Version.VERSION_1);
datePickerDialog.show(getFragmentManager(), "Datepickerdialog");
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: " + dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
birthDayEdt.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
public void getData() {
dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_country);
countryRecyler = (RecyclerView) dialog.findViewById(R.id.countryRecyclerView);
countryProgress = (ProgressBar) dialog.findViewById(R.id.countryDialog_progress);
countryRecyler.setLayoutManager(new LinearLayoutManager(context));
countryRecyler.setHasFixedSize(true);
countryProgress.setVisibility(View.VISIBLE);
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<CountryResponse> call = api.getCountryList();
call.enqueue(new Callback<CountryResponse>() {
@Override
public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) {
try {
if (response.body() != null) {
models.clear();
models.addAll(response.body().getData());
countryProgress.setVisibility(View.GONE);
countryRecyler.setAdapter(mAdapter);
}
} catch (Exception e) {
}
}
@Override
public void onFailure(Call<CountryResponse> call, Throwable t) {
}
});
dialog.show();
}
}
如何在单击国家/地区名称( 来自适配器 )时,为registerCountryEdtTxt.setText
( 在mainActivity中 )设置此名称? 我该怎么办?
我是业余爱好者,请帮助我<3
在适配器上创建接口以传输数据
public class CountryAdapter extends RecyclerView.Adapter {
public interface onListClickedRowListner {
void onListSelected(int mposition);
}
}
并在适配器构造函数中
onListClickedRowListner listner;
public CountryAdapter(List<CountryDatum> mData, Context context,onListClickedRowListner listner) {
this.mData = mData;
this.context = context;
this.listner = listner;
}
并在onBindViewHolder中
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof DataViewHolder) {
((DataViewHolder) holder).countryListTxt.setText(mData.get(position).getName() + "");
((DataViewHolder) holder).countryListTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "" + mData.get(position).getId(), Toast.LENGTH_SHORT).show();
listner.onListSelected(position);
}
});
}
}
并在mainActivity和onListSelected中实现此列表器,在此方法中,您可以使用该位置从mData获取值并将其分配给活动中的任何视图。
public class RegisterActivity extends AppCompatActivity implements
CountryAdapter.onListClickedRowListner {
.
.
.
@Override
public void onListSelected (int listposition){
Log.d("Tag",""+listposition);
}
}
在你身上创造这样的变化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Initialize
ButterKnife.bind(this);
context = RegisterActivity.this;
mAdapter = new CountryAdapter(models, context,this);
}
试试这个解决方案 在您的活动类中执行此操作。
countryRecyler.setAdapter(mAdapter,registerCountryEdtTxt);
然后在适配器类中
private List<CountryDatum> mData;
private Context context;
private EditText mEditText;
public CountryAdapter(List<CountryDatum> mData, Context
context,EditText edittext) {
this.mData = mData;
this.context = context;
mEditText=edittext;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof DataViewHolder) {
((DataViewHolder) holder).countryListTxt.setText(mData.get(position).getName() + "");
((DataViewHolder) holder).countryListTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "" + mData.get(position).getId(), Toast.LENGTH_SHORT).show();
mEditText.setText(mData.get(position).getName())
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.