删除android中的列表项表单列表视图

时间:2012-01-29 08:52:33

标签: android list

我读了很多更多关于这个问题,但没有得到更多的想法。在此之后,我想发布,在这张图片中我有3个项目列表,我有2个项目点击。所以我想删除这两个选中的项目。但我是Android的新手,所以无法在这背后得到更多的想法。

  

代码

 public class CountryList extends Activity implements OnClickListener,
    OnItemClickListener {
private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return tempCountry.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.country);

            holder.checkBox = (CheckedTextView) convertView
                    .findViewById(android.R.id.checkbox);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtcnt.setText(country[position]);
        return convertView;
    }
    static class ViewHolder {
        TextView txtcnt;
        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();
    }
    lv = (ListView) findViewById(R.id.list);
            btn_delete = (Button) findViewById(R.id.delete);
    btn_delete.setOnClickListener(this);
    checkbox = (CheckBox) findViewById(R.id.checkbox);
    txtname= (TextView) findViewById(R.id.body);

    String name= pref.getString("name", "");
    country= name.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 builder = new AlertDialog.Builder(this);
        builder .setMessage("Are you Sure want to delete checked country ?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // how to remove country

                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        alert.setTitle("Delete Country");
        alert.show();
    case R.id.checkbox:
     //What is the procedue in this section
    default:
        break;
    }
}
public void onItemClick(AdapterView<?> pareant, View view, int position,
        long id) {

    try {
        // I have trying this but could not proper output or only errors
                 SparseBooleanArray sp = lv.getCheckedItemPositions();
        /*String str = "";
        for (int i = 0; i < sp.size(); i++) {
            str += country[sp.keyAt(i)] + ",";

        }*/

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

这是唯一的三个国家,实际上,我有超过数百个国家。

2 个答案:

答案 0 :(得分:2)

tempCountry删除项目,然后拨打adapter.notifyDataSetChanged()

答案 1 :(得分:1)

在列表上有一个按钮,让它在xml中点击功能 喜欢先占位置

    public void OnClickButton(View V){
  final int postion = listView.getPositionForView(V);
    System.out.println("postion selected is : "+postion);
Delete(postion);

}

    public void Delete(int position){
        if (adapter.getCount() > 0) {

            //Log.d("largest no is",""+largestitemno);
            //deleting the latest added by subtracting one 1 
            comment = (GenrricStoring) adapter.getItem(position);
            //Log.d("Deleting item is: ",""+comment);
            dbconnection.deleteComment(comment);
            List<GenrricStoring> values = dbconnection.getAllComments();
            //updating the content on the screen
            this.adapter = new UserItemAdapter(this, android.R.layout.simple_list_item_1, values);
            listView.setAdapter(adapter);
        }
        else
        {
            int duration = Toast.LENGTH_SHORT;
            //for showing nothing is left in the list
            Toast toast = Toast.makeText(getApplicationContext(),"Db is empty", duration);
            toast.setGravity(Gravity.CENTER, 0, 0);

            toast.show();
        }

    }