在android的listview中,没有要删除的选中项
我试图在android中删除listview中的选中项,但我还没有做到,为什么?我的代码如下。请回复。我也尝试过这段代码,但没有得到更多的想法。 以及更多与从列表视图中删除列表项相关的内容在android的listview中,没有要删除的选中项,android,android-listview,Android,Android Listview,我试图在android中删除listview中的选中项,但我还没有做到,为什么?我的代码如下。请回复。我也尝试过这段代码,但没有得到更多的想法。 以及更多与从列表视图中删除列表项相关的内容 public class BookmarksJokes extends Activity implements OnClickListener, OnItemClickListener { ListView lv; static ArrayList<Integer> checks=new
public class BookmarksJokes extends Activity implements OnClickListener,
OnItemClickListener {
ListView lv;
static ArrayList<Integer> checks=new ArrayList<Integer>();
static String[] tempTitle = new String[100];
static String[] tempBody = new String[100];
static String[] pos = new String[100];
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return tempTitle.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.bookmarks_list_item,
null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.titleJok);
holder.text2 = (TextView) convertView
.findViewById(R.id.bodyJok);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
checks.set(position, 1);
}
else{
checks.set(position, 0);
}
}
});
holder.text1.setText(tempTitle[position]);
holder.text2.setText(tempBody[position]);
return convertView;
}
class ViewHolder {
TextView text1;
TextView text2;
CheckBox checkBox;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bokmarksjoks);
try {
db = (new DatabaseHelper(this)).getWritableDatabase();
} catch (IOException e) {
e.printStackTrace();
}
setUpViews();
for(int b=0;b<tempTitle.length;b++){
checks.add(b,0); //Assign 0 by default in each position of ArrayList
}
String one = pref.getString("title", "");
String two = pref.getString("body", "");
tempTitle = one.split(",");
tempBody = two.split(",");
lv.setAdapter(new EfficientAdapter(this));
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.delete:
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you Sure want to delete all checked jok ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
for(int i=0;i<checks.size();i++){
if(checks.get(i)==1){
Log.d(TAG, "i Value >>"+i);
checks.remove(i);
// i--;
Log.d(TAG, "checked Value >>"+checks);
Log.d(TAG, "i Value -- >>"+i);
}
}
((EfficientAdapter)lv.getAdapter()).notifyDataSetChanged();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
alert.setTitle("Delete Jokes");
alert.show();
case R.id.checkbox:
default:
break;
}
}
public类BookmarksJokes扩展活动实现OnClickListener,
麦克利克监听器{
ListView lv;
静态ArrayList检查=新建ArrayList();
静态字符串[]testitle=新字符串[100];
静态字符串[]tempBody=新字符串[100];
静态字符串[]pos=新字符串[100];
私有静态类EfficientAdapter扩展BaseAdapter{
私人停车场;
公共效率适配器(上下文){
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回title.length;
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.bookmarks\u列表项,
无效);
holder=新的ViewHolder();
holder.text1=(TextView)convertView
.findviewbyd(R.id.titleJok);
holder.text2=(TextView)convertView
.findviewbyd(R.id.bodyJok);
holder.checkBox=(checkBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
如果(((复选框)v).isChecked()){
检查。设置(位置,1);
}
否则{
检查。设置(位置,0);
}
}
});
holder.text1.setText(试探[位置]);
holder.text2.setText(tempBody[位置]);
返回视图;
}
类视图持有者{
TextView text1;
TextView text2;
复选框;
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bokmarks);
试一试{
db=(新DatabaseHelper(this)).getWritableDatabase();
}捕获(IOE异常){
e、 printStackTrace();
}
setupview();
对于(int b=0;b您在遍历列表时正在从列表中删除项。至少您必须确保在计数器变量中考虑删除的项和列表大小(这就是为什么原始代码中有一个i--,但您已经注释掉了它)
即,删除索引为2的项目后,列表中的下一个仍然是2,而不是3
取消注释应修复的i-。要删除选中项,但不修改列表的数据源,请从检查列表加载数据源。如下所示:
public class BookmarksJokes extends Activity implements OnClickListener,
OnItemClickListener {
ListView lv;
static ArrayList<Integer> checks=new ArrayList<Integer>();
static ArrayList<String> tempTitle = new String[100];
static ArrayList<String> tempBody = new String[100];
static String[] pos = new String[100];
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return tempTitle.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.bookmarks_list_item,
null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.titleJok);
holder.text2 = (TextView) convertView
.findViewById(R.id.bodyJok);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
checks.set(position, 1);
}
else{
checks.set(position, 0);
}
}
});
holder.text1.setText(tempTitle.get(position));
holder.text2.setText(tempBody.get(position));
return convertView;
}
class ViewHolder {
TextView text1;
TextView text2;
CheckBox checkBox;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bokmarksjoks);
try {
db = (new DatabaseHelper(this)).getWritableDatabase();
} catch (IOException e) {
e.printStackTrace();
}
setUpViews();
for(int b=0;b<tempTitle.size();b++){
checks.add(b,0); //Assign 0 by default in each position of ArrayList
}
String one = pref.getString("title", "");
String two = pref.getString("body", "");
String[] tokens = one.split(",");
tempTitle=Arrays.asList(tokens);
tokens= two.split(",");
tempBody =Arrays.asList(tokens);
lv.setAdapter(new EfficientAdapter(this));
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.delete:
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you Sure want to delete all checked jok ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
for(int i=0;i<checks.size();i++){
if(checks.get(i)==1){
Log.d(TAG, "i Value >>"+i);
checks.remove(i);
tempTitle.remove(i);
tempBody.remove(i);
// i--;
Log.d(TAG, "checked Value >>"+checks);
Log.d(TAG, "i Value -- >>"+i);
}
}
((EfficientAdapter)lv.getAdapter()).notifyDataSetChanged();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
alert.setTitle("Delete Jokes");
alert.show();
case R.id.checkbox:
default:
break;
}
}
public类BookmarksJokes扩展活动实现OnClickListener,
麦克利克监听器{
ListView lv;
静态ArrayList检查=新建ArrayList();
静态ArrayList Testitle=新字符串[100];
静态ArrayList tempBody=新字符串[100];
静态字符串[]pos=新字符串[100];
私有静态类EfficientAdapter扩展BaseAdapter{
私人停车场;
公共效率适配器(上下文){
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回title.length;
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.bookmarks\u列表项,
无效);
holder=新的ViewHolder();
holder.text1=(TextView)convertView
.findviewbyd(R.id.titleJok);
holder.text2=(TextView)convertView
.findviewbyd(R.id.bodyJok);
holder.checkBox=(checkBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
如果(((复选框)v).isChecked()){
检查。设置(位置,1);
}
否则{
检查。设置(位置,0);
}
}
});
holder.text1.setText(testitle.get(position));
holder.text2.setText(tempBody.get(position));
返回视图;
}
类视图持有者{
TextView text1;
TextView text2;
复选框;
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bokmarks);
试一试{
db=(新DatabaseHelper(this)).getWritableDatabase();
}捕获(IOE异常){
e、 printStackTrace();
}
setupview();
对于(int b=0;bI在“”上有面..checks.lenght和holder.text1.setText(checks[position]);holder.text2.setText(checks[position]);“”错误此>>。表达式的类型必须是数组类型,但解析为ArrayListHere,temp.remove(position);在这一行中,您已经通过了位置,您在其中声明了这一点。这是id吗?我尝试了id,但注意到工作正常。实际上,它不是id,它是i,我没有编译代码,所以它可能仍然有错误。根据您的代码,有些更改但不起作用>>>列表temp=Arrays.asList(attentile);temp.remove(pos);attentile=(String[])temp.toArray();