2017-10-20 159 views
0

它是我第一次嘗試使用RecyclerView。我的應用程序請求獲取文檔數據數組。我編寫了自己的適配器,用於從AsyncTask擴展的RecyclerView,TaskAsync類。什麼時候請求我需要的所有數據,但我不知道爲什麼這些數據不會顯示在我的回收商視圖中。請幫助。我閱讀了許多關於recyclerView的文章,但我認爲在我的適配器中出現了問題。RecyclerView不顯示數據

行爲類,併爲該類我有TaskAsync

public class Acts extends Fragment{ 

private String direction, limit, existingToken; 
private SharedPreferences sharedPreferences; 
private ArrayList<Document> documentsList; 
private TextView docCategory; 
private EditText search; 

private RecyclerView recyclerView; 
private RecyclerView.LayoutManager layoutManager; 
private DocumentAdapter documentAdapter; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    View act = inflater.inflate(R.layout.two_sided_docs, container, false); 
    View header = getActivity().getLayoutInflater().inflate(R.layout.document_list_header, null); 
    docCategory = (TextView)header.findViewById(R.id.docs_category_one); 
    search = (EditText)header.findViewById(R.id.search_text_one); 

    recyclerView = (RecyclerView) act.findViewById(R.id.documentListView2); 
    documentsList = new ArrayList<Document>(); 
    documentAdapter = new DocumentAdapter(act.getContext(), documentsList, Document.DOCUMENT_TYPE.ACT); 
    layoutManager = new LinearLayoutManager(act.getContext()); 
    recyclerView.setAdapter(documentAdapter); 
    recyclerView.setLayoutManager(layoutManager); 

    sharedPreferences = getContext().getSharedPreferences(Constants.PROJECT, Context.MODE_PRIVATE); 
    existingToken = sharedPreferences.getString(Constants.TOKEN, ""); 
    direction = "in"; 
    limit = "50"; 
    new TestAsync(getActivity()).execute(Urls.ACTS_FEED); 
    return act; 
} 

public class TestAsync extends AsyncTask<String, Void, String> { 
    private Activity activity; 
    private ProgressDialog progressDialog; 
    private OkHttpClient okHttpClient; 
    private Request request; 
    private RequestBody formBody; 

    public TestAsync(Activity activity) { 
     this.activity = activity; 
     progressDialog = new ProgressDialog(this.activity); 
    } 




    @Override 
    protected String doInBackground(String... params) { 

     Log.i("sadasd", "asdasd"); 
     URL url; 

     try { 
      url = new URL(params[0]); 

      okHttpClient = new OkHttpClient(); 

      formBody = new FormBody.Builder() 
        .add(Constants.DIRECTION, Constants.IN) 
        .add(Constants.LIMIT, Constants.docs_limit) 
        .build(); 

      request = new Request.Builder() 
        .url(url.toString()) 
        .addHeader(Constants.AUTH_TOKEN, existingToken) 
        .post(formBody) 
        .build(); 

      Response response = null; 
      response = okHttpClient.newCall(request).execute(); 
      return response.body().string(); 


     } catch (MalformedURLException e) { 
      Log.i("test", "test1"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.i("test", "test2"); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog.setTitle(R.string.wait); 
     progressDialog.setMessage("load"); 
     progressDialog.setCancelable(false); 
     progressDialog.show(); 
     Log.i("test", "test3"); 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     Log.i("test", "test4" + s); 
     AlertView.showAlertView(activity, Messages.CONNECTION_ERROR, s, Messages.OK); 
     if (progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
     } else if (s == null) { 
      Log.i("test", "test5"); 
      AlertView.showAlertView(activity, Messages.CONNECTION_ERROR, Messages.NO_INTERNET, Messages.OK); 
     } else { 
      Log.i("test", "test6"); 
      try { 
       Log.i("test", "test7"); 
       JSONObject jsonObject = new JSONObject(s); 
       int code = Integer.valueOf(jsonObject.getString(Constants.CODE)); 
       if (code == Codes.OK) { 
        Log.i("test", "test8"); 
        String header = jsonObject.getString("act_header"); 
        JSONArray arr = new JSONArray(header); 

        for (int i = 0; i < arr.length(); i++) { 
         JSONObject jsonPart = arr.getJSONObject(i); 

         Document document = null; 

         String docId; 
         String docNum; 
         String docTotalPrice; 
         String senderCompany; 
         String receiverCompany; 
         BigInteger timestamp; 
         String docStatus; 
         String documentInternalType; 

         docId = jsonPart.getString("id"); 
         docNum = jsonPart.getString("act_num"); 
         docTotalPrice = jsonPart.getString("total_price"); 
         senderCompany = jsonPart.getString("sender_company_name"); 
         receiverCompany = jsonPart.getString("receiver_company_name"); 
         timestamp = BigInteger.valueOf(Long.valueOf(jsonPart.getString("timestamp"))); 
         docStatus = jsonPart.getString("status"); 
         documentInternalType = jsonPart.getString("documentInternaltyep"); 

         document = new Document(docId, docNum, docTotalPrice, senderCompany, receiverCompany, timestamp, docStatus, documentInternalType); 
         Log.i("document", document.toString()); 
         documentsList.add(document); 
         documentAdapter.notifyDataSetChanged(); 

        } 
       } else { 
        Log.i("error", "error"); 
        AlertView.showAlertView(activity, Messages.ERROR, jsonObject.getString(Constants.MESSAGE), Messages.OK); 
       } 
      } catch (JSONException e) { 
       AlertView.showAlertView(activity, Messages.CONNECTION_ERROR, Messages.NO_INTERNET, Messages.OK); 
       e.printStackTrace(); 
      } 

     } 
    } 

} 

}

這是我DocumentAdapter類:

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

private Context context; 
private ArrayList<Document> docData; 
private Document.DOCUMENT_TYPE documentType; 
private static LayoutInflater inflater = null; 

public static class ViewHolder extends RecyclerView.ViewHolder { 
    TextView datetime; 
    TextView senderOrReceiverCompany; 
    TextView docInfo; 
    TextView docCost; 
    ImageView arrow; 
    TextView docStatus; 

    ViewHolder(View v) { 
     super(v); 

     datetime = (TextView) v.findViewById(R.id.datetime); 
     senderOrReceiverCompany = (TextView) v.findViewById(R.id.senderc); 
     docInfo = (TextView) v.findViewById(R.id.docInfo); 
     docCost = (TextView) v.findViewById(R.id.cost); 
     arrow = (ImageView) v.findViewById(R.id.arrow_icon); 
     docStatus = (TextView) v.findViewById(R.id.doc_st_sign); 
    } 
} 

public DocumentAdapter(Context context, ArrayList<Document> docData, Document.DOCUMENT_TYPE documentType) { 
    this.context = context; 
    this.docData = docData; 
    this.documentType = documentType; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    Context cn = parent.getContext(); 
    LayoutInflater inf = LayoutInflater.from(context); 

    View documentView = inf.inflate(R.layout.list_adapter_row, parent, false); 

    ViewHolder viewHolder = new ViewHolder(documentView); 

    return viewHolder; 
} 

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

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    Document document = docData.get(position); 

    TextView datetime; 
    TextView senderOrReceiverCompany; 
    TextView docInfo; 
    TextView docCost; 
    ImageView arrow; 
    TextView docStatus; 

    datetime = holder.datetime; 
    datetime.setText(document.getTimestamp()); 

    senderOrReceiverCompany = holder.senderOrReceiverCompany; 
    senderOrReceiverCompany.setText(document.getSenderCompany()); 

    docInfo = holder.docInfo; 
    docInfo.setText(String.format(Constants.ACT_TITLE, document.getDocNum(), document.getTimestamp())); 

    docCost = holder.docCost; 

    docStatus = holder.docStatus; 

    switch (documentType) { 
     case ACT: 
      docCost.setText(String.valueOf(Math.round(Double.valueOf(document.getDocTotalPrice()))) + Constants.KZT); 
      break; 
     case SHIPPING_LIST: 
      docCost.setText(String.valueOf(Math.round(Double.valueOf(document.getDocTotalPrice()))) + Constants.KZT); 
      break; 
     case COMMON_DOCUMENT: 
      docCost.setText(" "); 
      break; 
     case INVOICE: 
      docCost.setText(String.valueOf(Math.round(Double.valueOf(document.getDocTotalPrice()))) + Constants.KZT); 
      String colorful = ""; 
      switch (document.getDocStatus()) { 
       case "0": 
        colorful = Constants.DOCUMENT_INVOICE_STATUS.get(document.getDocStatus()); 
        break; 
       case "1": 
        colorful = "<font color='#008000'>" + Constants.DOCUMENT_INVOICE_STATUS.get(document.getDocStatus()) + "</font>"; 
        break; 
       case "2": 
        colorful = "<font color='#FFA500'>" + Constants.DOCUMENT_INVOICE_STATUS.get(document.getDocStatus()) + "</font>"; 
        break; 
       case "3": 
        colorful = "<font color='#EE0000'>" + Constants.DOCUMENT_INVOICE_STATUS.get(document.getDocStatus()) + "</font>"; 
        break; 
       default: 
        colorful = "<font color='#EE0000'>" + Constants.DOCUMENT_INVOICE_STATUS.get(document.getDocStatus()) + "</font>"; 
        break; 
      } 
      docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INVOICE_TYPE.get(document.getDocumentInternalType()) + " | " + colorful)); 
      break; 
     case PAYMENT_INVOICE: 
      docCost.setText(String.valueOf(Math.round(Double.valueOf(document.getDocTotalPrice()))) + Constants.KZT); 
      break; 
     case RECON: 
      docCost.setText(" "); 
      break; 
     default: 
      docCost.setText(document.getDocTotalPrice()); 
      break; 
    } 

    if (!documentType.equals(Document.DOCUMENT_TYPE.INVOICE)) { 
     switch (document.getDocStatus()) { 
      case "1": 
       docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INTERNAL_TYPE.get(document.getDocumentInternalType()) + " | " + "<font color='#800080'>" + Constants.DOCUMENT_STATUS.get(document.getDocStatus()) + "</font>")); 
       break; 
      case "2": 
       docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INTERNAL_TYPE.get(document.getDocumentInternalType()) + " | " + "<font color='#800080'>" + Constants.DOCUMENT_STATUS.get(document.getDocStatus()) + "</font>")); 
       break; 
      case "3": 
       docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INTERNAL_TYPE.get(document.getDocumentInternalType()) + " | " + "<font color='#FFA500'>" + Constants.DOCUMENT_STATUS.get(document.getDocStatus()) + "</font>")); 
       break; 
      case "4": 
       docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INTERNAL_TYPE.get(document.getDocumentInternalType()) + " | " + "<font color='#008000'>" + Constants.DOCUMENT_STATUS.get(document.getDocStatus()) + "</font>")); 
       break; 
      case "6": 
       docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INTERNAL_TYPE.get(document.getDocumentInternalType()) + " | " + "<font color='#EE0000'>" + Constants.DOCUMENT_STATUS.get(document.getDocStatus()) + "</font>")); 
       break; 
      case "7": 
       docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INTERNAL_TYPE.get(document.getDocumentInternalType()) + " | " + "<font color='#EE0000'>" + Constants.DOCUMENT_STATUS.get(document.getDocStatus()) + "</font>")); 
       break; 
      default: 
       docStatus.setText(Html.fromHtml(Constants.DOCUMENT_INTERNAL_TYPE.get(document.getDocumentInternalType()) + " | " + "<font color='#800080'>" + Constants.DOCUMENT_STATUS.get(document.getDocStatus()) + "</font>")); 
       break; 

     } 

    } 

    arrow = holder.arrow; 
} 

private Context getContext() { 
    return context; 
} 

}

這是我的佈局:

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

UPDATE 這就是我請求後得:Log.i("test", "test4" + s)沒有任何顯示到Android的監控

test: test4{"code": 0, "act_header": [{"status": 1, "sender_company_name": "\u0422\u041e\u041e\"\u0410\u0441\u0435\u043c-2\"", "receiver_company_name": "\u0422\u041e\u041e\"\u0410\u0441\u0435\u043c-2\"", "total_price": "1.000000", "create_date": "10.05.2017", "descri... 

,我不知道爲什麼我的數據沒有按't記錄

+0

你的ArrayList中有任何數據 docData? RecyclerView和適配器似乎沒關係... –

回答

0

通過getActivity()作爲你的上下文在所有的片段類。嘗試改變它,如下面的代碼。
這條線:

documentAdapter = new DocumentAdapter(getActivity(), documentsList, Document.DOCUMENT_TYPE.ACT); 

這條線:

layoutManager = new LinearLayoutManager(getActivity()); 

這行:

sharedPreferences = getActivity().getSharedPreferences(Constants.PROJECT, Context.MODE_PRIVATE); 
+0

它並不重要! –

+0

@BakhtiyarBekbergen你是什麼意思「沒關係!」? –

+0

@MilosLulic將getContext()更改爲getActivity()。仍然沒有工作 –

0

我認爲這個問題是這一行

documentAdapter = new DocumentAdapter(act.getContext(), documentsList, Document.DOCUMENT_TYPE.ACT); 

她e,你正在傳遞沒有任何數據的空列表。並且在getItemCount()方法中的Adapter中,您將得到的大小爲0.因此沒有要顯示的數據。

試試這個。

在onPostExecute()方法中,在解析數據的for循環之後調用此行。

documentAdapter = new DocumentAdapter(act.getContext(), documentsList, Document.DOCUMENT_TYPE.ACT); 
documentAdapter.notifyDataSetChanged(); 

可能會解決您的問題。

+0

不,問題不在這裏)我解決了這個問題),但感謝那裏的答案:) –

+0

是什麼問題? –

+0

請看onPostExecute方法 –