gpt4 book ai didi

java - 我如何从适配器获取数据并在 Android 的 Activity 中显示

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:10 25 4
gpt4 key购买 nike

在我的应用程序中,我想在dialog 中显示国家/地区。在我的应用程序中,mainActivity 中有一些 editTexts,当单击 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);
}
}

}

主要 Activity 代码:

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(in mainActivity)设置这个名称?我该怎么办?

我是业余的,请帮助我<3

最佳答案

In adapter create on interface to transfer data

public class CountryAdapter extends RecyclerView.Adapter {
public interface onListClickedRowListner {
void onListSelected(int mposition);
}
}

and in adapter constructor

onListClickedRowListner listner;
public CountryAdapter(List<CountryDatum> mData, Context context,onListClickedRowListner listner) {
this.mData = mData;
this.context = context;
this.listner = listner;

}

and in 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);
}
});
}
}

and implements this listner in mainActivity and onListSelected in this method you get position using that position get value from mData and assign to any view in your activity.

public class RegisterActivity extends AppCompatActivity implements 
CountryAdapter.onListClickedRowListner {
.
.
.

@Override
public void onListSelected (int listposition){
Log.d("Tag",""+listposition);
}
}

and in you oncreate change like this

@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);

}

关于java - 我如何从适配器获取数据并在 Android 的 Activity 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com