似乎无法将JsonArray放入ListView

时间:2016-09-30 14:31:51

标签: android arrays listview

我似乎无法将JsonArray放入ListView。我使用的是Custom ArrayAdapter,但仿真器屏幕上显示的只是整个JsonArray

此活动包含JsonArrayArrayAdapterViewHolder

的整个迭代
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。我不完全确定你是否愿意看到这一点,但如果需要,我很乐意将其添加到问题中。

1 个答案:

答案 0 :(得分:1)

如果您的ReadHttpTask将运行Http任务并返回数据charSequence。你解析你的json对象错误,应该像这样(它不是数组的对象)

JSONObject reader = new JSONObject(charSequence.toString());
JSONArray jsonArray = reader.optJSONArray("catches");