40

这是我为列表设置自定义适配器的片段的代码。

没有错误,但ListView为空。我已经实现getCount()了它在我的 ArrayList 中返回正确数量的项目。("Inside", "GetView")我在 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;
    }

}
4

6 回答 6

166

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

  1. getCount返回 0。
  2. 你忘记打电话setAdapterListView
  3. 如果ListView的可见性(或其容器的可见性)是GONE. 感谢@TaynãBonaldo 的宝贵意见。
  4. ListView未附加到任何视口布局。也就是说,mListView = new ListView(...)不使用myLayout.addView(mListView).

onPostExcute您创建新实例后,CarListAdapter我会建议您将新实例更新到您的ListView. 确实你需要再打电话

 mList.setAdapter(adapter);

编辑:setAdapter应始终在 ui 线程上调用,以避免意外行为

编辑2:

这同样适用于RecyclerView。确保

  • getItemCount正在返回一个大于0(通常是数据集大小)的值
  • 两者setLayoutManagersetAdapter必须在UI Thread
  • 小部件的可见性必须设置为VISIBLE
于 2013-05-02T12:42:25.270 回答
5

在将项目添加到列表时,您必须验证列表是否包含元素可能会出错。要验证 ,请使用以下方法:

adapter.getCount();
于 2015-02-03T17:07:09.930 回答
3

我遇到了类似的问题。这是解决它的简单方法:

在您的 onCreateView 中,您必须等待创建视图。所以改变你的台词:

mList = (ListView)v.findViewById(R.id.list);
mList.setAdapter(adapter);

将以上两行更改为:

mList = (ListView)v.findViewById(R.id.list);
mList.post(new Runnable() {
    public void run() {
        mList.setAdapter(adapter);
    }
});

希望这会帮助其他遇到类似问题的人

于 2013-11-30T10:18:32.817 回答
0

您在构造函数中缺少超类。请参阅下面的示例:

public AppDataAdapter(Activity a, int textViewResourceId, ArrayList<AppData> entries) {
    super(a, textViewResourceId, entries);
    this.entries = entries;
    this.activity = a;
}
于 2013-05-02T12:44:13.750 回答
0

你一直在做的是

在您的适配器中

public CarListAdapter(Context context , ArrayList<CarDetail> items) {

    this.context = context;
    this.items = items;

}

在你的片段中

adapter = new CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);

我希望你会使用FragmentActivity

你需要打电话

adapter = new CarListAdapter(YOUR_ACTIVITY_CONTEXT, carDetailList);

YOUR_ACTIVITY_CONTEXT你会在哪里FragmentActivity

于 2013-05-02T12:54:34.957 回答
0

我有同样的问题。在尝试了上面的所有提示之后,我的 getView 仍然没有被调用。所以我试图删除我在 ListView 之外使用的 ScrollView。然后 getView 运行良好。只是为了增加一种可能性。我希望能帮助别人。

于 2018-06-28T12:57:13.527 回答