gpt4 book ai didi

android - 如何从列表中删除复选框项目

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:40 25 4
gpt4 key购买 nike

我在列表中有 50 个列表项。现在我已经选中了 10 个项目,那么当我单击删除按钮时如何从 ListView 中删除(删除)这 10 个选中的项目。

这是我的代码

请查看我的代码和响应错误在哪里:

public class BookmarksJokes extends Activity implements OnClickListener,
OnItemClickListener {
ListView lv;
Button btn_delete;
public String TAG = "horror";
private SQLiteDatabase db;
public static final String PREFS_NAME = "MyPreferences";
static String[] tempTitle = new String[100];
static String[] tempBody = new String[100];
private static boolean bRequiresResponse;

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(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.title);
holder.text2 = (TextView) convertView
.findViewById(R.id.body);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.text1.setText(tempTitle[position]);
holder.text2.setText(tempBody[position]);
// bRequiresResponse = checkBox.isChecked();
return convertView;
}

static 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();

title = (TextView) findViewById(R.id.body);
SharedPreferences pref = getSharedPreferences(PREFS_NAME, 0);

//String ids = pref.getString("jid", "");
String one = pref.getString("title", "");
String two = pref.getString("body", "");

tempTitle = one.split(",");
tempBody = two.split(",");
lv.setAdapter(new EfficientAdapter(this));
}

private void setUpViews() {
lv = (ListView) findViewById(R.id.list);
btn_delete = (Button) findViewById(R.id.delete);
btn_delete.setOnClickListener(this);
// checkbox = (CheckBox) findViewById(R.id.checkbox);

}
private void removeData(){
//int pos= getIntent().getIntExtra("POSITION", 0);
//lv.removeViewAt(pos);
// notifyAll();*/
// int pos= getIntent().getIntExtra("POSITION", 0);
// if(checkBox.isChecked()){
// Log.d(TAG, " checked d]"+pos);
// Toast.makeText(this, "checked "+pos, Toast.LENGTH_SHORT).show();
// }
}
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) {
//removeJok();

}
})
.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 void onItemClick(AdapterView<?> arg0, View view, int position,
long ids) {

try {

// checkBox = (CheckBox)view.findViewById(R.id.checkbox);
// checkBox.setChecked(true);

Intent intent = new Intent(BookmarksJokes.this,
BookmarkJokesDetails.class);

intent.putExtra("POSITION", position);
intent.putExtra("ID", ids);
Cursor cursor = (Cursor) adapter.getItem(position);

intent.putExtra("_ID",
cursor.getInt(cursor.getColumnIndex("_id")));

startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}

Toast.makeText(BookmarksJokes.this,
"Item in position " + position + " clicked", Toast.LENGTH_LONG)
.show();
}
}

这是我的完整代码:

http://pastebin.com/LB2WKHMP

最佳答案

我在我的一个应用程序中执行了相同的任务。

你应该做的是使用一个与列表项大小相同的ArrayList,默认情况下每个项目包含0。现在,在您的 Efficient Adapter 中,为选中的每个复选框在 arrayList 中的特定位置分配 1。最后单击 delete 按钮,遍历 ArrayList 并从列表中删除所有那些在该 ArrayList 中那个位置包含 1 的项目,最后调用 notifyDatasetChanged() 列表的方法 以便列表得到刷新和显示新列表。

下面是一些示例代码,可以让您对此有更好的了解:

 ArrayList<Integer> checks=new ArrayList<Integer>();

现在在onCreate方法中

for(int b=0;b<tempTitle.length;b++){
checks.add(b,0); //assign 0 by default in each position of ArrayList
}

现在在您的 efficientAdapter 的 getView 方法中

 public View getView(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.title);
holder.text2 = (TextView) convertView
.findViewById(R.id.body);
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) {
// TODO Auto-generated method stub

if(((CheckBox)v).isChecked()){
checks.set(position, 1);
}
else{
checks.set(position, 0);
}

}
});


return convertView;
}

最后点击删除按钮:

     delete.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

for(int i=0;i<checks.size();i++){

if(checks.get(i)==1){
//remove items from the list here for example from ArryList
checks.remove(i);
//similarly remove other items from the list from that particular postion
i--;
}
}
((EfficientAdapter)lv.getAdapter()).notifyDataSetChanged();
}
}

希望这能解决您的问题。

关于android - 如何从列表中删除复选框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9016739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com