当我单击任何RecyclerView列表项时,如何在MainActivity的EditText中显示该特定单击的项?

时间:2019-04-15 07:38:38

标签: java android-recyclerview retrofit2 recycler-adapter

在这里,我已经使用翻新的api调用从服务器获取了一个由“状态名称”组成的列表,并将其显示在回收站视图中,但是当单击应在MainActivity的edittext中显示的任何状态名称时,我该如何这样做吗?

我尝试使用此链接在recyler视图中实现点击侦听器: 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类

公共类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;
}

}

0 个答案:

没有答案