这是我第一次尝试使用RecyclerView时。我的应用程序请求获取一系列文档数据。我为RecyclerView编写了自己的适配器,TaskAsync类从AsyncTask扩展而来。什么时候请求我需要的所有数据,但我不知道为什么这些数据不会在我的Recycler视图中显示。请帮助。我阅读了很多关于recyclerView的帖子,但我认为在我的适配器中出了问题。
使用Acts类进入此类我有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>
更新 这是我在请求后得到的:
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...
并且Log.i("test", "test4" + s)
之后没有任何内容显示在android监控中
我不知道为什么我的数据不记录
答案 0 :(得分:0)
在所有Fragment类中将getActivity()
作为您的Context传递。尝试更改它,如下面的代码
这一行:
documentAdapter = new DocumentAdapter(getActivity(), documentsList, Document.DOCUMENT_TYPE.ACT);
这一行:
layoutManager = new LinearLayoutManager(getActivity());
这一行:
sharedPreferences = getActivity().getSharedPreferences(Constants.PROJECT, Context.MODE_PRIVATE);
答案 1 :(得分:0)
我认为问题在于这一行
Author
在这里,您传递的空列表没有任何数据。在getItemCount()方法的Adapter中,您将获得大小为0.因此没有数据可以显示。
试试这个。
在onPostExecute()方法中,在你解析数据的for循环之后调用此行。
documentAdapter = new DocumentAdapter(act.getContext(), documentsList, Document.DOCUMENT_TYPE.ACT);
这可能会解决您的问题。
答案 2 :(得分:0)
就我而言,问题在于将RecyclerView的adapter
分配给了自己。
private lateinit var adapter: MyAdapter
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.recycler_view_layout, container, false)
val layoutManager = LinearLayoutManager(context)
adapter = MyAdapter(listOf(MyAdapter.Item(1, "dfg")))
view.recycler_view.apply {
this.layoutManager = layoutManager
this.adapter = adapter
this.setHasFixedSize(true)
}
return view
}
许多小时后,我删除了apply
运算符,并理解编译器将layoutManager
识别为局部变量,而adapter
被识别为recycler_view.adapter
。