我正在获取json对象,
" painWorse":"坐着,站着,,,,躺着"
我需要解析并显示为
目前我将文字设置为:
viewHolder.painWorse.setText(assessObject.get("painWorse").toString().replace("\"", ""));
我需要跳过空白项目并解析并仅显示已检查的项目。如何实现此目标
适配器代码:
public class AssessmentAdapter extends RealmBasedRecyclerViewAdapter<Assessment, AssessmentAdapter.ViewHolder> {
Context mContext;
public AssessmentAdapter(
Context context,
RealmResults<Assessment> realmResults,
boolean automaticUpdate,
boolean animateIdType) {
super(context, realmResults, automaticUpdate, animateIdType);
mContext = context;
}
public class ViewHolder extends RealmViewHolder {
//
@BindView(R.id.visitDate)
TextView visitDate;
@BindView(R.id.Name)
TextView name;
@BindView(R.id.txtAge)
TextView age;
@BindView(R.id.txtSex)
TextView sex;
@BindView(R.id.problemBegin)
TextView problem;
@BindView(R.id.problem)
TextView problemBegin;
@BindView(R.id.morningRate)
TextView morningRate;
@BindView(R.id.afternoonRate)
TextView afternoonRate;
@BindView(R.id.eveningRate)
TextView eveningRate;
@BindView(R.id.assessmentDetails)
CardView assessmentDetails;
@BindView(R.id.painWorse)
TextView painWorse;
@BindView(R.id.currentCondition)
TextView currentCondition;
@BindView(R.id.diagnosticStudied)
TextView diagnosticStudied;
@BindView(R.id.medications)
TextView medications;
@BindView(R.id.history)
TextView history;
@BindView(R.id.goals)
TextView goals;
public ViewHolder(View container) {
super(container);
ButterKnife.bind(this, container);
}
}
@Override
public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) {
ViewHolder vh = null;
try {
View v = inflater.inflate(R.layout.assessment_card_view, viewGroup, false);
vh = new ViewHolder(v);
} catch (Exception e) {
Log.v(Constants.TAG, "onCreateRealmViewHolder Exception: " + e.toString());
}
return vh;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindRealmViewHolder(ViewHolder viewHolder, int position) {
final Assessment assessment = realmResults.get(position);
try {
JsonParser parser = new JsonParser();
JsonArray assess = parser.parse(assessment.getAssesment().toString()).getAsJsonArray();
Log.v(Constants.TAG, "assessing" + assess);
JsonObject assessObject = assess.get(0).getAsJsonObject();
Log.v(Constants.TAG, "assessObject" + assessObject);
viewHolder.visitDate.setText(assessObject.get("Date").toString().replace("\"", ""));
viewHolder.name.setText(assessObject.get("Name").toString().replace("\"", ""));
viewHolder.age.setText(assessObject.get("Age").toString().replace("\"", ""));
viewHolder.sex.setText(assessObject.get("Sex").toString().replace("\"", ""));
viewHolder.problem.setText(assessObject.get("Problem").toString().replace("\"", ""));
viewHolder.problemBegin.setText(assessObject.get("ProblemBegin").toString().replace("\"", ""));
viewHolder.morningRate.setText(assessObject.get("morningRate").toString().replace("\"", ""));
viewHolder.afternoonRate.setText(assessObject.get("afternoonRate").toString().replace("\"", ""));
viewHolder.eveningRate.setText(assessObject.get("eveningRate").toString().replace("\"", ""));
viewHolder.painWorse.setText(assessObject.get("painWorse").toString().replace("\"", ""));
viewHolder.currentCondition.setText(assessObject.get("currentCondition").toString().replace("\"", ""));
viewHolder.diagnosticStudied.setText(assessObject.get("Diagnostic").toString().replace("\"", ""));
viewHolder.medications.setText(assessObject.get("Medications").toString().replace("\"", ""));
viewHolder.history.setText(assessObject.get("History").toString().replace("\"", ""));
viewHolder.goals.setText(assessObject.get("Goals").toString().replace("\"", ""));
viewHolder.assessmentDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
} catch (Exception e) {
Log.v(Constants.TAG, "Exception: " + e.toString());
}
}
}
答案 0 :(得分:3)
在不知道有关您的代码的任何其他内容的情况下,我会说您需要使用正则表达式来删除那些多个逗号
String pain = "Sitting,Standing,,,,Lying Down";
pain = pain.replaceAll(",+",","); // Replace every ocurrence of one or more commas with just one comma
// pain = "Sitting,Standing,Lying Down";
更清晰,更简单地格式化此信息的方法是使用LinearLayout
以TextView
作为项目尽可能多地填充painWorse
。所以一旦你能做到以下几点:
LinearLayout
更改为String[] items = pain.split(",");
垂直方向LinearLayout
以新的for (String item : items) {
TextView tv = new TextView(context);
tv.setText(item);
// any other styling here...
}
tracking table
id | labref | user_id
-----------------------
1 | a | 1
------------------------
2 | a | 3
------------------------
3 | b | 4
------------------------
5 | b | 7
------------------------
答案 1 :(得分:0)
您可以使用GSON / Jackson来解析JSON对象。你会发现很多注释可以在这些库中排除EMPTY,NULL等。它们还提供了更容易阅读的代码
此处给出了使用GSON的示例
https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/