1

I have used checkbox for selecting the fields as in image belowcheckbox image

I am getting json object as,

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

I need to parse and display as

  • Sitting
  • Standing
  • Lying Down

Currently i am setting the text as:

viewHolder.painWorse.setText(assessObject.get("painWorse").toString().replace("\"", ""));

I need to skip the empty items and parse and display only the checked items.How to achieve this

Adapter code:

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

    }


}
4
  • can you share the code by which you are trying to parse? and on what action or event? Commented Jun 14, 2017 at 6:08
  • viewHolder.painWorse.setText(assessObject.get("painWorse").toString().replace("\"", "")); Commented Jun 14, 2017 at 6:12
  • here i am parsing the contents from painWorse objects.. I am getting output as Sitting,Standing,,,,Lying Down . I need remove empty string Commented Jun 14, 2017 at 6:15
  • i have updated code please check Commented Jun 14, 2017 at 6:18

2 Answers 2

3

Without knowing anything else about your code I would say that you need to use a regex in order to remove those multiple commas

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

A clearer and easier way of formatting this info would be using a LinearLayout dinamically filled with as much TextViews as items. So once you can do the following:

  1. Change your painWorse into a LinearLayout with vertical orientation
  2. Separate your String into several items String[] items = pain.split(",");
  3. Fill dinamically your newly LinearLayout

    for (String item : items) {
        TextView tv = new TextView(context);
        tv.setText(item);
        // any other styling here...
    }
    
Sign up to request clarification or add additional context in comments.

5 Comments

Nice, I am getting as Sitting,Standing,Lying Down. How to display these contents as list
If you want to split a String into several items you should check some other questions
Thanks bro, I fixed using viewHolder.painWorse.setText( " \u2022 " + TextUtils.join("\n \u2022 ", pain.toString().replace("\"", "").replace("[", "").replace("]", "").split(",")));
Seems like a workaround to me. Consider using a LinearLayout instead of your painWorse TextView. This way you can dinamically add as much TextViews as you need at runtime (and even format it in a better and clearer way)
You're welcome! As a general rule think that "As a user it looks good" is not the same as "As a developer that need to mantain the code it looks good". So try to achieve both objectives ;)
0

You can use GSON/ Jackson to parse your JSON objects. You will find plenty of annotations to exclude EMPTY, NULL etc. etc. in these libraries. Also they provide a much easier code to read

An example for using GSON is given here

https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

2 Comments

Sure, will go through and check.Thanks
not yet bro. I need to display as a list only the checked items

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.