Android getView()未调用

时间:2015-09-26 16:22:47

标签: android listview android-fragments getview

我使用ActionBar Tabbed View创建了一个peroject 在那需要自定义ListView In片段 但getView方法没有被调用

ArrayAdapter类

    public class MySimpleArrayAdapter extends ArrayAdapter<ChildPojo> {

        static class ViewHolder {
            public TextView text1;
            public TextView text2;
        }

        private final Context context;
        private final List<ChildPojo> values;
        public MySimpleArrayAdapter(Context context,List<ChildPojo> values) {
            super(context, R.layout.item_list, values);
            this.context = context;
            this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.i("class/getView", "not getting called");
            View rowView = convertView;
            if (rowView == null) {
                rowView = LayoutInflater.from(this.getContext())
                        .inflate(R.layout.item_list, parent, false);
                // configure view holder
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.text1 = (TextView) rowView.findViewById(R.id.item_label);
                viewHolder.text2 = (TextView) rowView.findViewById(R.id.item_price);


                rowView.setTag(viewHolder);
            }

            ViewHolder holder = (ViewHolder) rowView.getTag();


            String s = values.get(position).getNamec();
            holder.text1.setText(s);
            int i = values.get(position).getPrice();
            holder.text2.setText(String.format("%d",i));


            return rowView;
        }

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

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

    }


FragmentClass extends List Fragment

    DAOClass dao;
    List<ChildPojo> childList = new ArrayList<ChildPojo>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final List<ChildPojo> data = new ArrayList<ChildPojo>();
        data.add(new ChildPojo(1,"name 1",100,1));
        data.add(new ChildPojo(2,"name 2",200,1));
        MySimpleArrayAdapter adapter = new              MySimpleArrayAdapter(getActivity(),data);

         setListAdapter(adapter);
    }

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

        View view = inflater.inflate(R.layout.row_item, container, false);
        return view;
    }
}

2 个答案:

答案 0 :(得分:0)

我假设你在onCreateView

中夸大了错误的观点

在FragmentClass的onCreateView方法中,像这样编辑下面的行

View view = inflater.inflate(R.layout.R.layout.item_list, container, false);
SimpleArrayAdapterClass中的

像这样更改下面的行

rowView = LayoutInflater.from(this.getContext())
            .inflate(R.layout.row_item, parent, false);

答案 1 :(得分:0)

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

        View view = inflater.inflate(R.layout.row_item, container, false);
  final List<ChildPojo> data = new ArrayList<ChildPojo>();
        data.add(new ChildPojo(1,"name 1",100,1));
        data.add(new ChildPojo(2,"name 2",200,1));
        MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(getActivity(),data);

         setListAdapter(adapter);
        return view;
    }