回收者视图不显示值

时间:2018-08-21 20:37:33

标签: android android-volley

我正在对一个项目进行一些测试。我正在使用Volley来消费Web服务并在ReciclerView中显示数据。我得到了答复,但它们未显示在小部件中。我已经举杯祝酒,以确认我确实收到了Web服务的响应。出现故障。在您从Web服务获取数据之前会显示该列表,为什么?

这是我的活动

public class ListMockio extends AppCompatActivity {

private RecyclerView mList;

private LinearLayoutManager linearLayoutManager;
private DividerItemDecoration dividerItemDecoration;
private List<Movie> movieList;
private RecyclerView.Adapter adapter;

private String url = "http://www.mocky.io/v2/5b7af6c73400005f008ed7b2"; // LisT varios

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


    mList = (RecyclerView) findViewById(R.id.main_list);

    movieList = new ArrayList<>();
    adapter = new MovieAdapter(getApplicationContext(),movieList);

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

    mList.setHasFixedSize(true);
    mList.setLayoutManager(linearLayoutManager);
    mList.addItemDecoration(dividerItemDecoration);
    mList.setAdapter(adapter);

    // Call to web Service
    getData();
}

private void getData() {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++)
                try {
                    JSONObject jsonObject = response.getJSONObject(i);

                    Movie movie = new Movie();
                    movie.setTitle(jsonObject.getString("title"));
                    movie.setRating(jsonObject.getInt("rating"));
                    movie.setYear(jsonObject.getInt("releaseYear"));

                    Toast.makeText(getApplicationContext(), "Respuesta " + jsonObject.getString("title"), Toast.LENGTH_SHORT).show();


                    movieList.add(movie);
                } catch (JSONException e) {
                    e.printStackTrace();
                    progressDialog.dismiss();
                }
            adapter.notifyDataSetChanged();
            progressDialog.dismiss();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());
            progressDialog.dismiss();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}

}

适配器代码

    public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    private Context context;
    private List<Movie> list;

    public MovieAdapter(Context context, List<Movie> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.single_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Movie movie = list.get(position);

        holder.textTitle.setText(movie.getTitle());
        holder.textRating.setText(String.valueOf(movie.getRating()));
        holder.textYear.setText(String.valueOf(movie.getYear()));

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textTitle, textRating, textYear;

        public ViewHolder(View itemView) {
            super(itemView);

            textTitle = (TextView) itemView.findViewById(R.id.main_title);
            textRating = (TextView) itemView.findViewById(R.id.main_rating);
            textYear = (TextView) itemView.findViewById(R.id.main_year);
        }
    }

}

single_item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/main_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textSize="18sp"/>


    <TextView
        android:id="@+id/main_rating"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Rating"/>

    <TextView
        android:id="@+id/main_year"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Year"/>
</LinearLayout>

还有我的ReciclerView activity_list_mockio.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    android:id="@+id/main_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v7.widget.RecyclerView>

结果

enter image description here

1 个答案:

答案 0 :(得分:0)

在声明中的ListMockio类中,更改:

private RecyclerView.Adapter adapter;

收件人:

private MovieAdapter adapter;

希望这可以解决您的问题。

相关问题