0

我正在学习 Android,我正在尝试使用自定义 ArrayAdapter 设置一个有点通用的 ListFragment。我不断收到错误消息:

adapter= new myAdapter(context, list);

这是我的 ListFragment:

public class sideFragment<T extends models.Listable> extends ListFragment{
@Override
public void onStart() {
    super.onStart();
    adapter= new myAdapter(context, list);
}
public void setList(List<T> t){
    list=t;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adapter = new myAdapter (getActivity(),list);       
}

还有我的 ArrayAdapter:

public class myAdapter extends ArrayAdapter<T>{
    Context context;
    List<T> list;
    public myAdapter(Context context, List<T> list){
        super(context, android.R.id.content, list);
        this.context=context;
        this.list=list;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View view = vi.inflate(R.layout.list_item, parent);

        if(view == null){
            Log.e("SIDEFRAG","VIEW NOT CREATED");
        }
        T t = list.get(position);

        ImageButton image= (ImageButton) view.findViewById(R.id.image_button);
        TextView tv1 = (TextView) view.findViewById(R.id.text_view1);
        TextView tv2 = (TextView) view.findViewById(R.id.text_view2);

        tv1.setText(t.getName());
        tv2.setText(t.getDescription());

        if (t instanceof models.Formation){
            image.setBackgroundColor(android.graphics.Color.BLACK);
        } else
            image.setBackgroundColor(android.graphics.Color.WHITE);             
        return view;
    }
      }     
}

我对Android真的很陌生,请帮助我理解我做错了什么?

谢谢

4

1 回答 1

0

解决方案是 getCount() 返回 0 for myAdapter。所以我只是使用mAdapter.add(temp)然后mAdapter.notifyDataSetChanged()它起作用了。我还不得不将线路更改row = inflater.inflate(R.id.pager, parent, false);row = inflater.inflate(R.layout.listview_row, parent, false);

于 2013-08-24T05:31:31.680 回答