【问题标题】:Android volley recyclerview not showing dataAndroid volley recyclerview 不显示数据
【发布时间】:2017-07-22 15:41:04
【问题描述】:

我花了很多时间调试这段代码,但不幸的是找不到错误,谁能帮我找到这段代码中的问题。我无法在活动页面上看到数据。虽然我发现我正在从服务器获取响应数据。

活动类

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>

【问题讨论】:

  • 你得到的输出是什么?
  • 我看到一个丢失的adapter.notifyDataSetChanged();
  • @AjilO。黑屏,SelectServiceActivity 页面加载时没有数据。我已经检查了没有可见性=隐藏/消失
  • @Pavneet_Singh 不会每次都创建一个新的适配器吗?
  • @SachinBahukhandi 这就是重点,不需要notify

标签: android android-recyclerview android-volley


【解决方案1】:

来自docs

必须为 RecyclerView 提供 LayoutManager 才能正常工作。

您需要添加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);
    }

改进:

  • Item类中初始化serviceType并在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()));
      //^^^^^^^^
    
    }
    

【讨论】:

  • 先生,我也在做同样的事情
  • 您添加了layoutManager,正如我使用^^^^^ 符号在上面的代码中指出的那样
  • ((项目)持有人).serviceTypeIssue.setText(selectServiceList.get(position).getServiceTypeIssue());
  • Recyclerview 还是空的?
  • onBindViewHolder 的这一行抛出错误“找不到局部变量 'holder'”
猜你喜欢
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 2017-04-15
  • 1970-01-01
  • 2016-08-25
相关资源
最近更新 更多