android - 如何从android中的json对象中删除和解析空字符串并显示为列表

标签 android json android-studio string-parsing

我使用复选框来选择字段,如下图所示 checkbox image

我正在获取 json 对象,

"painWorse":"Sitting,Standing,,,,Lying Down"

我需要解析并显示为

  • 坐着
  • 站立
  • 躺着

目前我将文本设置为:

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

    }


}

最佳答案

在对您的代码一无所知的情况下,我会说您需要使用正则表达式来删除那些多个逗号

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";

格式化此信息的一种更清晰、更简单的方法是使用动态填充与项目一样多的 TextViewLinearLayout。因此,一旦您可以执行以下操作:

  1. 将您的painWorse更改为垂直方向的LinearLayout
  2. 将您的字符串分成几个项目 String[] items = pain.split(",");
  3. 动态填充您的新 LinearLayout

    for (String item : items) {
        TextView tv = new TextView(context);
        tv.setText(item);
        // any other styling here...
    }
    

关于android - 如何从android中的json对象中删除和解析空字符串并显示为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44536711/

相关文章:

json - Swift 2 中的 HTTP 请求和条件

android - Kotlin 多平台用于开发 Mobile SDK?

java - Android BLE onScanResult 从未被调用

javascript - 从服务器向客户端获取/获取数据时出现问题。 (客户端向服务器发送数据成功)

javascript - JSON 未定义属性 - 简化验证以避免类型错误

android - 字符串中的自动完成功能未在Android Studio(Kotlin)上显示

清理 gradle 缓存后,Android Studio 无法解析符号

Android imagebutton 在点击时不闪烁

android - GIPHY Android SDK 在发布版本中不返回任何 GIFS

android - 如果 IP 地址不可用,如何无线调试我的应用程序?