fix: add placeholder data and error handling for nursing info display

- Pre-populate nursingInfoList with placeholder to prevent empty RecyclerView
- Add try-catch around JSON parsing in initNursingInfo
- Add toast on parse failure

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
renjianbo
2026-05-23 01:29:43 +08:00
parent fe5a2f2fdc
commit 7d56ff4bfe
7 changed files with 347 additions and 20 deletions

View File

@@ -4,6 +4,7 @@ public class homeListBean {
private String pic;
private String content;
private String url;
private String articleBody;
public String getPic() {
return pic;
@@ -28,4 +29,12 @@ public class homeListBean {
public void setUrl(String url) {
this.url = url;
}
public String getArticleBody() {
return articleBody;
}
public void setArticleBody(String articleBody) {
this.articleBody = articleBody;
}
}

View File

@@ -62,6 +62,14 @@ public class HomeFragment extends BaseFragment {
mContentView = inflater.inflate(R.layout.fragment_home_layout, container, false);
reply_rcey = (RecyclerView) mContentView.findViewById(R.id.marking_fragment_recyclerView);
robCustomerInfoBean = new robCustomerInfoBean();
// 先填充占位数据,避免 inner RecyclerView 高度为 0 导致护理资讯区域不显示
List<homeListBean> placeholder = new ArrayList<>();
homeListBean pb = new homeListBean();
pb.setContent("正在加载...");
pb.setPic("");
pb.setUrl("");
placeholder.add(pb);
robCustomerInfoBean.setNursingInfoList(placeholder);
initRecyclerView(robCustomerInfoBean);
//创建一个过滤器对象
IntentFilter intentFilter= new IntentFilter();
@@ -158,23 +166,29 @@ public class HomeFragment extends BaseFragment {
public void onNext(@NonNull Response<String> response) {
String body = (String) response.body();
Log.e("护理资讯", body);
JSONObject json = JSON.parseObject(body);
JSONObject data = json.getJSONObject("data");
if (data != null) {
JSONArray realTimeInfoList = data.getJSONArray("realTimeInfoList");
if (realTimeInfoList != null) {
List<homeListBean> list = new ArrayList<>();
for (int i = 0; i < realTimeInfoList.size(); i++) {
JSONObject item = realTimeInfoList.getJSONObject(i);
homeListBean bean = new homeListBean();
bean.setContent(item.getString("title"));
bean.setPic(item.getString("realTimeInfoImage"));
bean.setUrl(item.getString("realTimeInfoUrl"));
list.add(bean);
try {
JSONObject json = JSON.parseObject(body);
JSONObject data = json.getJSONObject("data");
if (data != null) {
JSONArray realTimeInfoList = data.getJSONArray("realTimeInfoList");
if (realTimeInfoList != null && realTimeInfoList.size() > 0) {
List<homeListBean> list = new ArrayList<>();
for (int i = 0; i < realTimeInfoList.size(); i++) {
JSONObject item = realTimeInfoList.getJSONObject(i);
homeListBean bean = new homeListBean();
bean.setContent(item.getString("title"));
bean.setPic(item.getString("realTimeInfoImage"));
bean.setUrl(item.getString("realTimeInfoUrl"));
bean.setArticleBody(item.getString("content"));
list.add(bean);
}
robCustomerInfoBean.setNursingInfoList(list);
adapter.notifyDataSetChanged();
}
robCustomerInfoBean.setNursingInfoList(list);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.e("护理资讯", "解析失败", e);
toast("护理资讯数据异常");
}
}

View File

@@ -67,8 +67,11 @@ public class ViewHolderFive extends AbstractViewTypeHolder {
return;
}
String url = item.getUrl();
String articleBody = item.getArticleBody();
if (url != null && !url.isEmpty()) {
XfiveWebActivity.runActivity(mContext, "了解陪护", url);
} else if (articleBody != null && !articleBody.isEmpty()) {
XfiveWebActivity.runActivityWithContent(mContext, item.getContent(), articleBody);
} else {
XfiveWebActivity.runActivity(mContext, "了解陪护", "file:///android_asset/privacy.html");
}

View File

@@ -18,6 +18,7 @@ import com.ruilaizi.service.utils.X5WebView;
public class XfiveWebActivity extends BaseActivity implements View.OnClickListener {
public final static String URL = "url";
public final static String TITLE = "title";
public final static String CONTENT = "content";
private RelativeLayout mLayTopLeftTv;
private TextView mLayTopTitle;
private X5WebView webview;
@@ -29,14 +30,22 @@ public class XfiveWebActivity extends BaseActivity implements View.OnClickListen
context.startActivity(intent);
}
public static void runActivityWithContent(Context context, String title, String htmlContent) {
Intent intent = new Intent(context, XfiveWebActivity.class);
intent.putExtra(TITLE, title);
intent.putExtra(CONTENT, htmlContent);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xfive);
String url = getIntent().getStringExtra(URL);
String content = getIntent().getStringExtra(CONTENT);
String title = getIntent().getStringExtra(TITLE);
initView(title);
initData(url);
initData(url, content);
}
@@ -54,7 +63,7 @@ public class XfiveWebActivity extends BaseActivity implements View.OnClickListen
}
private void initData(String url) {
private void initData(String url, String htmlContent) {
webview = (X5WebView) findViewById(R.id.webView);
webview.getView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
webview.addJavascriptInterface(new WebViewJavaScriptFunction() {
@@ -66,8 +75,11 @@ public class XfiveWebActivity extends BaseActivity implements View.OnClickListen
}
}, "Android");
//加载网页
webview.loadUrl(url);
if (htmlContent != null && !htmlContent.isEmpty()) {
webview.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);
} else if (url != null && !url.isEmpty()) {
webview.loadUrl(url);
}
}