不调用ArrayAdapter的getView()

时间:2014-12-12 17:31:14

标签: android json parsing android-arrayadapter android-adapter

我正在进行Flickr集成并使用Json解析..我能够获取数据但是它没有显示,因为适配器的getView()方法没有被调用。但是,我使用Toast消息检查了适配器类的构造函数..我不知道为什么getView()没有被调用...我已经检查了几乎类似的问题的其他问题,但无法找到解决我的问题的解决方案

public class FlickrImagesList extends Fragment
{
    private FragmentActivity myContext;

    ArrayList<Flickr> flickrList;

    private static final String photo = "photo";
   private static final String id = "id";
    private static final String owner = "owner";
    private static final String secret = "secret";
    private static final String server = "server";
    private static final String farm = "farm";
    private static final String title = "title";
    private static final String ispublic = "ispublic";
    private static final String isfriend = "isfriendt";
    private static final String isfamily = "isfamily";

ImageLoader imageLoader;
  FlickrListAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View rootView = inflater.inflate(R.layout.flickr_main_list, container, false);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
  //      imageLoader = ImageLoader.getInstance();
//        imageLoader.init(ImageLoaderConfiguration.createDefault(myContext));
        rootView.setFocusableInTouchMode(true);

        flickrList= new ArrayList<Flickr>();
            new JSONAsyncTask().execute("https://api.flickr.com/services/rest?method=flickr.groups.pools.getPhotos&api_key=xyz&format=json&group_id=2058557@N24&nojsoncallback=1");
        ListView listview = (ListView)rootView.findViewById(R.id.listflickr);
      adapter = new FlickrListAdapter(getActivity().getApplicationContext(), R.layout.flickr_list_single,flickrList);

    listview.setAdapter(adapter);


        return rootView;
    }
    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();

            dialog = new ProgressDialog(getActivity());
            dialog.setMessage("Loading, please wait");
            dialog.setTitle("Connecting server");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls)
        {
            try
            {


                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);


                int status = response.getStatusLine().getStatusCode();
                Log.e("status","status");
                if (status == 200) {
                    Log.e("status2","status2");
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);
                    entity.consumeContent();
                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("photos");

                    for (int i = 0; i < jarray.length(); i++)
                    {
                        Log.e("status3","status3");
                        JSONObject c = jarray.getJSONObject(i);

                            JSONObject ph = c.getJSONObject(photo);

                        Flickr actor = new Flickr();
                        if(ph.has(id)) {
                            actor.setId(ph.getString(id));
                        }
                        if(ph.has(owner)) {
                            actor.setOwner(ph.getString(owner));
                        }
                        if(ph.has(secret))
                        {
                            actor.setSecret(ph.getString(secret));
                        }
                        if(ph.has(server)) {
                            actor.setServer(ph.getString(server));
                        }
                        if(ph.has(farm)) {
                            actor.setFarm(ph.getString(farm));
                        }
                        if(ph.has(title)) {
                            actor.setTitle(ph.getString(title));
                        }
                        if(ph.has(ispublic)) {
                            actor.setIspublic(ph.getString(ispublic));
                        }
                        if(ph.has(isfriend)) {
                            actor.setIsfriend(ph.getString(isfriend));
                        }
                        if(ph.has(isfamily)) {
                            actor.setIsfamily(ph.getString(isfamily));
                        }
                        Log.e("actor.getServer()", actor.getServer());
 flickrList.add(actor);
                    }
                    return true;
                }

                //------------------>>

            }
            catch (ParseException e1)
            {
                e1.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (JSONException e)
            {
                Log.e("exception", e.getMessage());
                e.printStackTrace();
            }
            return false;
        }

        protected void onPostExecute(Boolean result)
        {

            dialog.cancel();
           adapter.notifyDataSetChanged();

        }

    }

}

FlickrListAdapter

public class FlickrListAdapter extends ArrayAdapter<Flickr> {
    ArrayList<Flickr> flickrList;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;
    DisplayImageOptions options;


    public FlickrListAdapter(Context context, int resource, ArrayList<Flickr> objects)
    {
        super(context, resource, objects);
        Toast.makeText(context,"FlickrListAdapter",Toast.LENGTH_LONG).show();
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        flickrList = objects;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(android.R.color.transparent)
                .showImageForEmptyUri(android.R.color.transparent)
                .showImageOnFail(android.R.color.transparent)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {    Toast.makeText(getContext(),"getView",Toast.LENGTH_LONG).show();
        // convert view = design
        View v = convertView;
        if (v == null)
        {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            holder.imageview = (ImageView) v.findViewById(R.id.flickr_image);

            v.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) v.getTag();
        }

        holder.imageview.setImageResource(R.drawable.logo_icon);


        ImageLoader.getInstance().displayImage("https://farm" + flickrList.get(position).getFarm() + ".staticflickr.com/" + flickrList.get(position).getServer() + "/"
                + flickrList.get(position).getId() + "_" + flickrList.get(position).getSecret() + ".jpg",holder.imageview, options);


    }

    static class ViewHolder
    {
        public ImageView imageview;
        public TextView textview;
        public TextView textview2;
    }


}

如果我添加

@Override
    public int getCount () {
        return 1;
    }

然后app崩溃显示ImageLoader上的错误

  java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

2 个答案:

答案 0 :(得分:1)

您的适配器getView()未被调用,因为您从flickrList获取的列表JSONAsyncTask()为空。

实际上你得到了这个例外:

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

根据你的.json回复 的 http://www.mocky.io/v2/548b349c9739deb61372aecf

问题是你的解析器,这将解决你的问题:

                JSONObject jsono = new JSONObject(data);
                JSONObject jb = jsono.getJSONObject("photos");
                JSONArray jarray = jb.getJSONArray("photo");

        for (int i = 0; i < jarray.length(); i++){
                    Log.e("status3","status3");
                    JSONObject ph = jarray.getJSONObject(i);

                    Flickr actor = new Flickr();

答案 1 :(得分:0)

我正在创建JSONObject的问题就是为什么列表的大小为零,因为jarray为零..我通过更改为...来解决它。

 JSONObject jsono = new JSONObject(data);
                        JSONObject c = jsono.getJSONObject(photos);
                        JSONArray jarray = c.getJSONArray(photo);

                for (int i = 0; i < jarray.length(); i++){
                    Log.e("status3","status3");
                    JSONObject ph = jarray.getJSONObject(i);

                    Flickr actor = new Flickr();