RecyclerView第二次单击不显示项目

时间:2019-10-15 17:03:36

标签: java android android-layout android-recyclerview

我有一个RecyclerView,用于显示从向远程api发出请求后获得的列表。该列表是在首次出现该活动时显示的,但是当我返回并再次回到该活动时,该列表不会显示。我该如何解决? 这就是我称为recyclerview的活动。

public class DisplayMessage extends AppCompatActivity {
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.language_selection);
        recyclerView = (RecyclerView) findViewById(R.id.recylerview);

        LanguageAdapter languageAdapter = new LanguageAdapter(this);
        recyclerView.setAdapter(languageAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

    }
}

这是Adapter类。

public class LanguageAdapter extends RecyclerView.Adapter<LanguageAdapter.MyViewHolder> {

    ArrayList<Language> languageArrayList;

    public LanguageAdapter(Context context) {
        this.languageArrayList = new ArrayList<Language>();
        RequestQueue queue = Volley.newRequestQueue(context);
        String url ="https://api.bounswe2019group9.tk/contents/languages";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray data = (JSONArray) response.get("data");
                            for(int i=0; i<data.length();i++){
                                Language lang = new Language((String) data.get(i));
                                languageArrayList.add(lang);
                                Log.i("api", ""+lang.getLanguageName());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }
                , new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("language_selection", "Error on request to get language list");

            }
        });
        queue.add(jsonObjectRequest);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.languages, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.languageName.setText(languageArrayList.get(position).getLanguageName());
    }

    @Override
    public int getItemCount() {
        return languageArrayList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView languageName;
        public RelativeLayout relativeLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.languageName = (TextView) itemView.findViewById(R.id.language);
            this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
        }

        @Override
        public void onClick(View v) {

        }


    }
}

2 个答案:

答案 0 :(得分:1)

创建活动时,

OnCreate()方法仅被调用一次。您已经在OnCreate()中使用语言列表设置了适配器,这就是为什么仅在第一次设置适配器的原因。当您在同一屏幕上重新输入时,OnCreate将不会再次被调用。

您必须在OnResume()方法中设置适配器,因为每次访问此屏幕时都会调用此方法。

答案 1 :(得分:0)

在您的Activity类中,覆盖 onResume 方法,该方法还声明以下代码-

   LanguageAdapter languageAdapter = new LanguageAdapter(this);
   recyclerView.setAdapter(languageAdapter);