我有一个由复选框列表填充的列表视图..我想添加一个按钮,当我按下它时,它会为列表视图中选择的每个项目执行某项操作..找到选中哪个复选框的最佳方法是什么?我知道我可以使用setOnCheckedChangeListener
,但是我如何为列表中的每个复选框调用 if?如果我创建一个数组字符串并且每次我的复选框选中它会将其文本添加到数组列表中,然后我可以为每个选定的项目执行循环吗?这是我的代码来填充列表视图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database_list);
File path=new File(ClubCP.SDcardPath);
List<String> file_lists = main.directoryPath(path);
ListView Database_list=(ListView)findViewById(R.id.database_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.database_list_item,R.id.chk_database_list_item,file_lists);
ArrayAdapter<String> adapter1 = new InteractiveArrayAdapter(this,file_lists);
Database_list.setAdapter(adapter1);
}
我搜索了,我想我必须使用一个 ArrayAdapter ..我正在写它的类..这是我的类包 co.tosca.persianpoem;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
public class InteractiveArrayAdapter extends ArrayAdapter<String> {
private final List<String> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<String> list) {
super(context, R.layout.database_list_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.database_list_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chk_database_list_item);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
///do sth
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
return view;
}
}
谢谢你的帮助