Reputation: 79
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.
Upvotes: 0
Views: 501
Reputation: 1473
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.
Upvotes: 2