0
JsonArray
をListView
に入れることに問題があるようです。 Custom ArrayAdapter
を使用していますが、エミュレータ画面に表示されるのはすべてJsonArray
です。ListViewにJsonArrayを入れることができないようです
このアクティビティはJsonArray
て全体の繰り返しを保持し、ArrayAdapter
と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();
}
}
カスタム・アダプタ
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);
}
}
ビューホルダー
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);
}
}
}
}
私はListView
のXMLとListViewのカスタムレイアウト用のXMLを持っています。私はあなたがそれを見たいと思っているかどうかは十分にはわかっていませんが、必要であれば質問に追加して喜んでします。
JSON配列全体がどのビューに表示されますか? – brandall