在我的应用程序中,我希望在 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 中的)设置此名称?我怎么能这样呢?
我是业余爱好者,请帮助我&lt; 3
答案 0 :(得分:1)
在接口上创建适配器以传输数据
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);
}