Android volley recyclerview没有显示数据

时间:2017-07-22 15:41:04

标签: android android-recyclerview android-volley

我花了很多时间调试这段代码,但遗憾的是找不到bug,有人可以帮我找到这段代码中的问题。我无法在活动页面上看到数据。虽然我发现我从服务器获得响应数据。

活动类

public class SelectServiceActivity extends AppCompatActivity {

    private TextView textView;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_service);

        recyclerView = (RecyclerView) findViewById(R.id.select_service_rv);

        String serviceTypeId = getIntent().getStringExtra(AppConstants.SELECTED_SERVICE_TYPE_ID);

        getProductDetailsByProductId(serviceTypeId);
    }

    private void getProductDetailsByProductId(String serviceTypeId) {

        final List<SelectServiceBean> selectServiceList = new ArrayList<>();
        final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                Request.Method.GET,
                APIEndpoints.SERVICE_LIST_URI + serviceTypeId,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        if (response.length() > 0) {
                            for (int i = 0; i < response.length(); i++) {
                                try {
                                    selectServiceList.add(DatabaseObjectsMapper.selectServiceBeanMapper((JSONObject) response.get(i)));
                                } catch (JSONException e) {
                                    Toast.makeText(SelectServiceActivity.this, "Something went wrong : " + e.getMessage(), Toast.LENGTH_LONG).show();
                                }
                            }
                            recyclerView.setAdapter(new SelectServiceAdapter(getApplicationContext(),selectServiceList));
                        } else {
                            Toast.makeText(SelectServiceActivity.this, "No Response From Server!", Toast.LENGTH_LONG).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(SelectServiceActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                });

        VolleySingleton.getInstance(SelectServiceActivity.this).addToRequestQueue(jsonArrayRequest);
    }
}

适配器类

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

    private Context context;
    private List<SelectServiceBean> selectServiceList;

    public SelectServiceAdapter(final Context context, final List<SelectServiceBean> selectServiceList) {
        this.context = context;
        this.selectServiceList = selectServiceList;
    }

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

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((Item) holder).serviceTypeIssue.setText(selectServiceList.get(position).getServiceTypeIssue());
        ((Item) holder).serviceTypeIssue.setText(selectServiceList.get(position).getServiceType());
    }

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

    public static class Item extends RecyclerView.ViewHolder {

        private TextView serviceTypeIssue;
        private TextView serviceType;

        public Item(View view) {
            super(view);
            serviceTypeIssue = view.findViewById(R.id.service_type_issue);

        }

    }
}

单一视图项

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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/service_type_issue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Sample text" />


        <android.support.v7.widget.AppCompatCheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1sp"
        android:background="@color/lightGrey" />

</LinearLayout>

Recyclerview文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/select_service_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

来自docs

  

必须提供LayoutManager才能使RecyclerView正常运行。

您需要添加layoutManager

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

        recyclerView = (RecyclerView) findViewById(R.id.select_service_rv);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        //^^^^^^^^^^^
        recyclerView.setLayoutManager(mLayoutManager);    
        //^^^^^^^^^^^^^^^^^^^^
        String serviceTypeId = getIntent().getStringExtra(AppConstants.SELECTED_SERVICE_TYPE_ID);

        getProductDetailsByProductId(serviceTypeId);
    }

改进:

  • serviceType课程中初始化Item并在onBindViewHolder中使用

    public Item(View view) {
        super(view);
        serviceTypeIssue = view.findViewById(R.id.service_type_issue);
        serviceType      = view.findViewById(R.id.yourID);
        //^^^^^^^
    }
    
  • 保留对new SelectServiceAdapter的引用,以便稍后您可以使用notify函数获得更佳效果

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
      ((Item)holder).serviceTypeIssue.setText(selectServiceList.get(position).getServiceTypeIssue());
      ((Item)holder).serviceType.setText(String.valueOf(selectServiceList.get(position).getServiceType()));
      //^^^^^^^^
    
    }