0

I think im going about this all wrong and just need a push in the right direction. I parsed my json information and set them to set fields, so each one can be called and displayed. Now this only works for 1 field at a time I can't load more than one using the adapter. Do I need to compile all of these arrays into one to be called via a custom adapter? Here is my code:

public class LocalJsonFileActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.localjsonfile)));
        StringBuilder jsonBuilder = new StringBuilder();
        for (String line = null; (line = jsonReader.readLine()) != null;) {
            jsonBuilder.append(line).append("\n");
        }

        JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
        JSONArray jsonArray = new JSONArray(tokener);


        ArrayList<String> name = new ArrayList<String>();
        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            name.add(jsonObject.getString("name"));
        }

        ArrayList<String> bloodtype = new ArrayList<String>();
        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            bloodtype.add(jsonObject.getString("bloodtype"));
        }

        ArrayList<String> type = new ArrayList<String>();
        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            type.add(jsonObject.getString("type"));

        }

        ArrayList<String> dob = new ArrayList<String>();
        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);

            String series = jsonObject.getString("dob");

            if (series.equals("December")) {
                dob.add(jsonObject.getString("dob"));
            }
        }

        setListAdapter(new ArrayAdapter<String>
                (this, R.layout.usercard, R.id.txttype, type));


    } catch (FileNotFoundException e) {
        Log.e("jsonFile", "file not found");
    } catch (IOException e) {
        Log.e("jsonFile", "ioerror");
    } catch (JSONException e) {
        Log.e("jsonFile", "error while parsing json");
    }
}

}

layout main is just a blank list view. My layout card has 4 fields 1 that is image view. But I can't seem to show more than 1 piece of information. Reading data in single fields and then outputting is great. I would just like to add the text to all fields.

3
  • Did you understand what I explained in my answer ? Commented Apr 2, 2018 at 2:04
  • Yes I did thank you for your advice. I am going to make a custom view adapter to pass the array to. Fingers crossed ill get it to work ;) thanks. Commented Apr 2, 2018 at 12:03
  • Cool. Happy coding! Commented Apr 2, 2018 at 12:25

1 Answer 1

2

If you want to pass multiple fields like this, you need to create an ArrayList of object type. Now which object ? Make a class like this -

public class PersonData {

    private String name, bloodType, type, dob;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBloodType() {
        return bloodType;
    }

    public void setBloodType(String bloodType) {
        this.bloodType = bloodType;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }
}

In your LocalJsonFileActivity, parse and store the data like this -

ArrayList<PersonData> data = new ArrayList<PersonData>();

        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            PersonData mPersonData = new PersonData();
            mPersonData.setName(jsonObject.getString("name"));
            mPersonData.setBloodType(jsonObject.getString("bloodtype"));
            mPersonData.setType(jsonObject.getString("type"));

            String series = jsonObject.getString("dob");

            if (series.equals("December")) {
                mPersonData.setDob(jsonObject.getString("dob"));
            }

            data.add(mPersonData);

        }

Use a custom adapter or modify the constructor of the adapter you are using to take data as type ArrayList<PersonData> as the parameter.

Then use these values in your adapter something like this -

holder.TextViewName.setText(data.get(position).getName());
holder.TextViewBloodType.setText(data.get(position).getBloodType());
holder.TextViewType.setText(data.get(position).getType());
holder.TextViewDOB.setText(data.get(position).getDob());

Don't copy the full code, try to understand how I did it and implement it in your project. Your implementation might be little different in the adapter, this is just a demo of how to do things like these.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that has helped. I will adapt my code and try to get it to work with this in mind.

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.