Reputation: 73
I seem to be having trouble putting a JsonArray
into ListView
. I am using a Custom ArrayAdapter
but all that comes out on the emulator screen is just the whole JsonArray
.
This activity holds the whole iteration through the JsonArray
, an ArrayAdapter
and a ViewHolder
public class ShowFishiesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_fishies);
Spinner spinner = (Spinner) findViewById(R.id.fishSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.fishSortArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
ReadTask task = new ReadTask();
task.execute("http://api.evang.dk/v1/catches");
}
private class ReadTask extends ReadHttpTask {
Adapter newAdapter;
List<Catch> catches;
@Override
protected void onPostExecute(CharSequence charSequence) {
super.onPostExecute(charSequence);
TextView messageText = (TextView) findViewById(R.id.messageText);
messageText.setText(charSequence);
catches = new ArrayList<>();
try {
JSONArray array = new JSONArray(charSequence.toString());
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
Integer id = obj.getInt("id");
String angler_name = obj.getString("angler_name");
String dateTime = obj.getString("datetime");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date jsonDate = sdf.parse(dateTime);
String fishingMethod = obj.getString("fishing_method");
String fishBreed = obj.getString("breed");
Double length = obj.getDouble("length");
Double weight = obj.getDouble("weight");
String weather = obj.getString("weather");
String location = obj.getString("location");
Double latitude = obj.getDouble("latitude");
Double longitude = obj.getDouble("longitude");
Catch fishCatch = new Catch(id, angler_name, jsonDate, fishingMethod, fishBreed, length, weight, weather, location, latitude, longitude);
catches.add(fishCatch);
ListView listView = (ListView) findViewById(R.id.fishListView);
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter(ShowFishiesActivity.this, android.R.layout.simple_expandable_list_item_1, catches);
//listView.setAdapter(arrayAdapter);
newAdapter = new Adapter();
listView.setAdapter(newAdapter);
}
} catch (JSONException ex) {
messageText.setText(ex.getMessage());
Log.e("Catches", ex.getMessage());
} catch (ParseException e) {
e.printStackTrace();
}
}
Custom Adapter
class Adapter extends ArrayAdapter<Catch> {
public Adapter() {
super(ShowFishiesActivity.this, android.R.layout.simple_list_item_1, catches);
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.activity_item_adapter, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(catches.get(position));
return (convertView);
}
}
View Holder
class ViewHolder {
TextView anglerName = null;
TextView date = null;
TextView fishingmethod = null;
TextView fishBreed = null;
TextView fishlength = null;
TextView fishWeight = null;
TextView fishWeather = null;
TextView fishLocation = null;
TextView fishLatitude = null;
TextView fishLongitude = null;
ViewHolder(View catchView){
anglerName = (TextView) catchView.findViewById(R.id.fishList_item_anglerName);
date = (TextView) catchView.findViewById(R.id.fishList_item_dateTime);
fishingmethod = (TextView) catchView.findViewById(R.id.fishList_item_fishingMethod);
fishBreed = (TextView) catchView.findViewById(R.id.fishList_item_fishBreed);
fishlength = (TextView) catchView.findViewById(R.id.fishList_item_length);
fishWeight = (TextView) catchView.findViewById(R.id.fishList_item_weight);
fishWeather = (TextView) catchView.findViewById(R.id.fishList_item_weather);
fishLocation = (TextView) catchView.findViewById(R.id.fishList_item_location);
fishLatitude = (TextView) catchView.findViewById(R.id.fishList_item_latitude);
fishLongitude = (TextView) catchView.findViewById(R.id.fishList_item_longitude);
}
void populateFrom(Catch catchFish){
anglerName.setText(catchFish.getAngler_name());
String str = String.format(String.valueOf(catchFish.getDateTime()));
date.setText(str);
fishingmethod.setText(catchFish.getSpearfishing());
fishBreed.setText(catchFish.getBreed());
String parseLength = Double.toString(catchFish.getLength());
fishlength.setText(parseLength);
String parseWeight = Double.toString(catchFish.getWeight());
fishWeight.setText(parseWeight);
fishWeather.setText(catchFish.getWeather());
fishLocation.setText(catchFish.getLocation());
String parseLatitude = Double.toString(catchFish.getLatitude());
fishLatitude.setText(parseLatitude);
String parseLongitude = Double.toString(catchFish.getLongitude());
fishLongitude.setText(parseLongitude);
}
}
}
}
I have an XML for the ListView
and an XML for the custom layout of the ListView. I am not entirely sure if you would like to see that but if needed, I would be happy to add it to the question.
Upvotes: 0
Views: 45
Reputation: 2954
if your ReadHttpTask will run Http task and return data charSequence
. Your parse your json object wrong, should be like this (it an object not an array)
JSONObject reader = new JSONObject(charSequence.toString());
JSONArray jsonArray = reader.optJSONArray("catches");
Upvotes: 1