Java – How do I get data from the adapter and display it in an activity in Android

How do I get data from the adapter and display it in an activity in Android… here is a solution to the problem.

How do I get data from the adapter and display it in an activity in Android

In my app, I want to display the country in the dialog. In my application, there are some editTexts in mainActivity, and when I click Contry editText, the countryDialog is displayed and sorts the countries in this dialog (I got the country from the server).

I

want to set the country on editText when I click on the county name.

My adapter code:

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

}

Main Activity Code:

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();
    }
}

When clicking on the country name (from the adapter), how do I set this name for registerCountryEdtTxt.setText (in mainActivity)? What am I going to do?

I am amateur, please help me < 3

Solution

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

}

Related Problems and Solutions