4 votes

Comment supprimer des cases à cocher de la liste ?

J'ai 50 éléments dans la liste. Maintenant, j'ai coché 10 éléments, alors comment faire pour que ces 10 éléments cochés soient supprimés de la liste lorsque je clique sur le bouton de suppression.

Voici mon code

Veuillez voir mon code et la réponse où se trouve l'erreur. :

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

Voici mon code complet :

http://pastebin.com/LB2WKHMP

0voto

MuhammadAamirALi Points 2555

Actualisez à nouveau la liste lorsque vous avez sélectionné la case à cocher et que vous avez appuyé sur le bouton de suppression. Cela signifie que vous devez à nouveau paramétrer l'adaptateur pour la vue en liste après que les éléments ont été supprimés. Merci de votre compréhension.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X