簡體   English   中英

如何從適配器獲取數據並在Android中的Activity中顯示

[英]how can i get data from adapter and show in Activity in Android

在我的應用程序中,我想在dialog顯示國家 在我的應用程序有一些editTextsmainActivity ,當點擊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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM