1

Over Here I have fetched a list consisting of 'state names' from server using retrofit api call and displayed it in a recycler view , but when clicked on any state name that should be displayed in edittext of the MainActivity , How can i do that???

i have tried to implement click listener in recyler view using this link: How to click recyclerview items in Activity?

public class MainActivity extends AppCompatActivity implements RecyclerItemClickListener.onItemClickListener {

    APIInterface apiInterface;
    RecyclerView recyclerView;
    private List<State> stateList;
    RecyclerView.Adapter adapter;
    LinearLayoutManager linearLayoutManager;
    private DividerItemDecoration dividerItemDecoration;
    private EditText editText;
    private TextView state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.edit_text);
        recyclerView = findViewById(R.id.main_list);
        stateList = new ArrayList<>();

        linearLayoutManager = new LinearLayoutManager(this);
       linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);     
  dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), 
       linearLayoutManager.getOrientation());


//null reference is occuring over here. 
//placing a text in editText.  

       editText.setText(state.getText().toString());

    apiInterface=NetworkClient.getRetrofitClient().create(APIInterface.class);

        getData();
    }


    private void getData() {

      Call<PostList> postList = apiInterface.getPostList();
        postList.enqueue(new Callback<PostList>() {
            @Override
            public void onResponse(Call<PostList> call, final Response<PostList> response) {

StatesAdapter(getApplicationContext(),response.body().getStates());

                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(linearLayoutManager);
                recyclerView.addItemDecoration(dividerItemDecoration);
                adapter = new StatesAdapter(MainActivity.this, response.body().getStates());




           recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this,recyclerView, new RecyclerItemClickListener.onItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {

                  state =(TextView) response.body().getStates();

}

                @Override
                public void onLongItemClick(View v, int position) {

                }
            }));

                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(Call<PostList> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
            }
        });
    }

//PostList class

public class PostList {

@SerializedName("code")
@Expose
private String code;
@SerializedName("States")
@Expose
private List<State> states = null;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public List<State> getStates() {
    return states;
}

public void setStates(List<State> states) {
    this.states = states;
}

}

Zeus
  • 11
  • 2

0 Answers0