未调用自定义适配器 getView() 方法 [英] Custom Adapter getView() method is not called

查看:31
本文介绍了未调用自定义适配器 getView() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我将自定义适配器设置到列表中的片段的代码.

Here is the code of the fragment in which I am setting a custom adapter to the list.

没有错误,但 ListView 为空.我已经实现了 getCount(),它在我的 ArrayList 中返回正确数量的项目.我在 logcat 中没有看到 ("Inside", "GetView")

There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. I don't see ("Inside", "GetView") in the logcat

片段

public class ServiceCarListFragment extends Fragment {

    private String url;
    private ArrayList<CarDetail> carDetailList = new ArrayList<CarDetail>();
    private CarListAdapter adapter;
    private ListView mList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        url = getActivity().getIntent().getStringExtra("url");
        new DownloadCarDetail().execute(url);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_service_car_list, container, false);
        mList = (ListView) v.findViewById(R.id.list);
        mList.setAdapter(adapter);

        for (CarDetail car : carDetailList) {
            // START LOADING IMAGES FOR EACH STUDENT
            car.loadImage(adapter);
        }
        return v;
    }

    class DownloadCarDetail extends AsyncTask<String, String, ArrayList<CarDetail>> {

        @Override
        protected ArrayList<CarDetail> doInBackground(String... params) {
            // TODO Auto-generated method stub
            ArrayList<CarDetail> carDetailList = JsonParser.parseJson(params[0]);
            return carDetailList;
        }

        @Override
        protected void onPostExecute(ArrayList<CarDetail> carDetailList) {
            // TODO Auto-generated method stub
            ServiceCarListFragment.this.carDetailList = carDetailList;
            Log.d("dccs", String.valueOf(ServiceCarListFragment.this.carDetailList.size()));
            adapter = new CarListAdapter(getActivity(), ServiceCarListFragment.this.carDetailList);
            Log.d("dccs", String.valueOf((adapter.getCount())));
        }

    }
}

自定义适配器

public class CarListAdapter extends BaseAdapter {

    private ArrayList<CarDetail> items = new ArrayList<CarDetail>();
    private Context context;

    public CarListAdapter(Context context, ArrayList<CarDetail> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("Inside", "GetView");
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        ViewHolder holder = null;
        CarDetail car = items.get(position);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.car_list_row, null);
            holder = new ViewHolder();
            holder.tvCarName = (TextView) convertView.findViewById(R.id.tvCarName);
            holder.tvDailyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
            holder.tvWeeklyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
            holder.imgCar = (ImageView) convertView.findViewById(R.id.imgCar);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvCarName.setText(car.getCarName());
        if (car.getImage() != null) {
            holder.imgCar.setImageBitmap(car.getImage());
        } else {
            // MY DEFAULT IMAGE
            holder.imgCar.setImageResource(R.drawable.ic_action_call);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView tvCarName;
        TextView tvDailyPriceValue;
        TextView tvWeeklyPriceValue;
        ImageView imgCar;
    }

}

推荐答案

没有调用 getView 的唯一原因是:

The only reasons getView is not called are:

  1. getCount 返回 0.
  2. 您忘记在 ListView 上调用 setAdapter.
  3. 如果 ListView 的可见性(或其容器的可见性)是 GONE.感谢@TaynãBonaldo 提供宝贵意见.
  4. ListView 未附加到任何视口布局.也就是说,mListView = new ListView(...) 是在没有 myLayout.addView(mListView) 的情况下使用的.
  1. getCount returns 0.
  2. you forget to call setAdapter on the ListView.
  3. If the ListView's visibility (or its container's visibility) is GONE. Thanks to @TaynãBonaldo for the valuable input.
  4. ListView is not attached to any viewport layout. That is, mListView = new ListView(...) is used without myLayout.addView(mListView).

onPostExcute 中,在您创建 CarListAdapter 的新实例后,我会建议您将新实例更新到您的 ListView.确实需要再次调用

In the onPostExcute, after you create a new instance of CarListAdapter I will suggest you to update the new instance to your ListView. Indeed you need to call again

 mList.setAdapter(adapter);

setAdapter 应始终在 ui 线程上调用,以避免出现意外行为

setAdapter should be always called on the ui thread, to avoid unexpected behaviours

编辑 2:

同样适用于 RecyclerView.确保

  • getItemCount 返回的值大于 0(通常是数据集大小)
  • setLayoutManagersetAdapter 都必须在 UI 线程
  • 上调用
  • 小部件的可见性必须设置为VISIBLE
  • getItemCount is returning a value grater than 0 (usually the dataset size)
  • both setLayoutManager and setAdapter have to be called on the UI Thread
  • The visibility of the widget has to be set to VISIBLE

这篇关于未调用自定义适配器 getView() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆