我創建了動作條分頁查看 一個peroject在需要定製的ListView在片段 但getView方法不獲取調用的Android 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;
}
}
還可以提供'R.layout.row_item'源代碼? –
檢查你的'R.layout.row_item'文件,根據這個文件必須包含'ListView'的id'@android:id/list' - [check docs](http://developer.android.com/參考/ android /應用程序/ ListFragment.html) –
fragment_main.xml –