2017-07-22 66 views
2

我花了很多時間調試此代碼,但不幸找不到該錯誤,有人可以幫我找到此代碼中的問題。我無法在活動頁面上看到數據。雖然我發現我從服務器獲取響應數據。Android volley recyclerview未顯示數據

活動類

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> 
+0

的輸出是什麼你得到notify的功能呢? –

+0

我看到缺少'adapter.notifyDataSetChanged();' –

+0

@AjilO。黑屏,SelectServiceActivity頁面加載時沒有數據。我已經檢查過沒有可見性=隱藏/去過 –

回答

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); 
    } 

改進:在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的引用,以便以後可以使用更多的最優性能

    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())); 
        //^^^^^^^^ 
    
    } 
    
+0

我正在做同樣的先生 –

+0

你添加了'layoutManager',因爲我在上面的代碼中使用了'^^^^^'符號 –

+0

yes ,看起來像我非常接近解決方案 –