我在main.xml中有一个listview,在一个名为options的不同xml中有一个复选框。 列表视图挂钩到custombaseadapter并填充项目。 我希望用户能够通过复选框从列表视图中删除和恢复项目(最好是在同一位置),方法是选中或取消选中它们。 我的问题是我如何使用baseadapter来做到这一点? 任何帮助和一些示例代码都非常感谢。 THX。
Baseadapter代码:
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.test, null);
holder = new ViewHolder();
holder.txtNaam = (TextView) convertView.findViewById(R.id.naam);
holder.txtWaarde = (TextView) convertView.findViewById(R.id.waarde);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtNaam.setText(searchArrayList.get(position).getNaam());
holder.txtWaarde.setText(searchArrayList.get(position).getWaarde());
return convertView;
}
static class ViewHolder {
TextView txtNaam;
TextView txtWaarde;
}
}
我的主xml,ready,ready1,ready2中的OnReadyListner是各个复选框的名称:
private class OnReadyListenerBeltegoed implements MyBeltegoed.ReadyListener {
public void ready(boolean niewbel) {
// TODO Auto-generated method stub
}
public void ready1(boolean vorige) {
// TODO Auto-generated method stub
}
public void ready2(boolean teg_tot) {
// TODO Auto-generated method stub
}
}
选项xml文件中的代码,其中复选框为:
public class MyBeltegoed extends Dialog {
public interface ReadyListener {
public void ready(boolean nieuwbel);
public void ready1(boolean vorige);
public void ready2(boolean teg_tot);
public void ready3(boolean verbruik_teg);
public void ready4(boolean ongebruik_teg);
public void ready5(boolean boven_bundel);
public void ready6(boolean niet_bundel);
}
private ReadyListener readyListener;
public MyBeltegoed(Context context, ReadyListener readyListener) {
super(context);
this.readyListener = readyListener;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.sencide.R.layout.beltegoed);
Button buttonOk = (Button) findViewById(R.id.btnBel);
buttonOk.setOnClickListener(new OKListener());
}
private class OKListener implements android.view.View.OnClickListener {
public void onClick(View v) {
CheckBox niewbel = (CheckBox) findViewById(R.id.nieuwbel);
readyListener.ready(niewbel.isChecked());
CheckBox vorige = (CheckBox) findViewById(R.id.vorige);
readyListener.ready1(vorige.isChecked());
}
}