未调用自定义 ArrayAdapter 的 getView

getView of custom ArrayAdapter not being called

类似的问题 Custom adapter getview is not called

我有一个自定义适配器可以将图像放在我的应用程序屏幕上,但它根本没有被调用。做一些研究我发现我需要重写 getCount() 方法。我已经 return 调整了 ArrayList 的大小,但仍然没有成功。我以为这个列表可能是空的,但通过调试,我可以确认它在传递给自定义 ArrayAdapter class 时不是空的。我什至尝试 return 在 getCount() 中设置 20 的值来查看是否可行,但仍然没有成功。

这是我到目前为止完成的代码;

MainActivityFragment.class

public class MainActivityFragment extends Fragment {
    private ImageAdapter imageAdapter;

    public class FetchMovieTask extends AsyncTask<String, Void, JSONObject> {
        @Override
        protected void onPostExecute(JSONObject strings) {


            Log.i(LOG_TAG, strings.toString());

            try {
                jsonArray = strings.getJSONArray("results");


            if (null != jsonArray) {

                ArrayList<JSONObject> list = new ArrayList<JSONObject>();

                if (jsonArray != null) {
                    int len = jsonArray.length();
                    for (int i=0;i<len;i++){
                        list.add((JSONObject) jsonArray.get(i));
                    }
                }

                imageAdapter = new ImageAdapter(getActivity(), list);
            }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

ImageAdapter.class

public class ImageAdapter extends ArrayAdapter<ArrayList> {

ArrayList list;
Context context;

public ImageAdapter(Context context, ArrayList list) {
    super(context, android.R.id.content);
    this.list = list;
    this.context = context;
}


@Override
public int getCount() {
    //return list.size();
    return 20;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    String IMAGE_BASE_URL = "http://image.tmdb.org/t/p/w185/";

    try {
        JSONObject jsonObject = (JSONObject) list.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_image_movie, parent, false);
        }

        ImageView iv = (ImageView) convertView.findViewById(R.id.list_movie_pics_imageview);

        Picasso.with(context)
                .load(IMAGE_BASE_URL + jsonObject.get("poster_path"))
                .into(iv);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return convertView;
}

}

有什么解决办法吗?

您必须在创建后调用 imageAdapter.notifyDataSetChanged()。这将通知适配器调用 getView()

更新 在 listView 上设置适配器,然后 notifyDataSetChanged

yourListView.setAdapter(imageAdapter); imageAdapter.notifyDataSetChanged()

初始化您的 Adapter 后,将其设置为您的 ListView

public class MainActivityFragment extends Fragment {

private ImageAdapter imageAdapter;

public class FetchMovieTask extends AsyncTask<String, Void, JSONObject> {
    @Override
    protected void onPostExecute(JSONObject strings) {


        Log.i(LOG_TAG, strings.toString());

        try {
            jsonArray = strings.getJSONArray("results");


        if (null != jsonArray) {

            ArrayList<JSONObject> list = new ArrayList<JSONObject>();

            if (jsonArray != null) {
                int len = jsonArray.length();
                for (int i=0;i<len;i++){
                    list.add((JSONObject) jsonArray.get(i));
                }
            }

            imageAdapter = new ImageAdapter(getActivity(), list);
            yourListView.setAdapter(imageAdapter);
        }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
  }
}

根据您的评论,我可以看到您正在将适配器分配给 onCreateView 方法中的 listview。那时您的适配器还没有准备好。所以你需要在向它添加数据后刷新你的适配器。

添加

imageAdapter.notifyDataSetChanged();

之后
imageAdapter = new ImageAdapter(getActivity(), list);

onPostExecute 方法中。