我正在学习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来说真的很新,请帮我理解我做错了什么?
谢谢
答案 0 :(得分:0)
解决方案是getCount()为myAdapter
返回0。所以我只使用mAdapter.add(temp)
然后使用mAdapter.notifyDataSetChanged()
并且它有效。我还必须将行row = inflater.inflate(R.id.pager, parent, false);
更改为row = inflater.inflate(R.layout.listview_row, parent, false);