我的应用中有ListView
adapter
,我用它来显示包含chechbox
和textView
public class MyListAdapter extends ArrayAdapter<Model> {
private LayoutInflater inflater;
private int position;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public MyListAdapter (Context context, List<Model> listMeasurement){
super(context, R.layout.simplerow, R.id.empty, listMeasurement);
inflater= LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent){
Model model= (Model)this.getItem(position);
CheckBox checkBox;
TextView textView;
}
}
我的问题是:
我想在另一个list
中展示另一个activity
,这将有一个image
,两个textViews
和一个button
。图像取决于textView
的值。
执行此操作的最佳方法是执行其他ArrayAdapter
?或使用其他东西?
提前致谢。
答案 0 :(得分:1)
您需要扩展BaseAdapter
并覆盖getView()
方法。这是一个样本。
private class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<Model> list;
public CustomAdapter(Context context, ArrayList<Model> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// If the view is null inflate it from xml
if (view == null)
view = inflater.inflate(R.layout.list_row, null);
// Bind xml to java
ImageView icon = (ImageView) view
.findViewById(R.id.image);
TextView text = (TextView) view.findViewById(R.id.text);
text.setText(list.get(position).getText());
icon.setImageDrawable(list.get(position).getDrawable());
return view;
}
}