在android中点击按钮,从数据库和列表视图中删除数据

时间:2015-11-06 09:35:50

标签: android-listview

我正在尝试从数据库中删除记录槽listview。我不知何故从列表中删除了记录,但没有找到任何帮助从数据库中删除它。 下面是我的适配器的代码。任何帮助将不胜感激。 提前谢谢。

public class Row_keyskill extends BaseAdapter implements View.OnClickListener {


    private ArrayList<String> listName=new ArrayList<String>();
    private Activity activity;
    private Context context;
    String id;
    SQLiteDatabase db;
    int position,position1;

    public Row_keyskill(Activity activity, ArrayList<String> listName) {
        super();
        this.listName = listName;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listName.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listName.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public void onClick(View v) {

        Toast.makeText(context,"row item clicked"+listName.get(position),Toast.LENGTH_LONG).show();
      /*  int position = (Integer) v.getTag();
        id = listName.get(position);
        // listName.remove(position);
        Log.e("Row_keyskill", "position id:  "+id);
        position1=position+1;
        Log.e("Row_keyskill", "position1 id:  "+position1);
        db.execSQL("DELETE FROM USER_SKILLS WHERE U_SKILL_NAME ='" + id + "'");
       // itemDelete();*/
    }

    public static class ViewHolder
    {

        public TextView txtName;
        public ImageView btn_delete;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.rowdata_keyskill,null);

            view.txtName = (TextView) convertView.findViewById(R.id.keyskill_row);
           // view.btn_delete=(ImageView)convertView.findViewById(R.id.btn_delete);

           // view.btn_delete.setTag(position);


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

        view.txtName.setText(listName.get(position));
        //view.btn_delete.setOnClickListener(this);

        ImageView btn_delete=(ImageView)convertView.findViewById(R.id.btn_delete);
        btn_delete.setTag(position);
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something

                int position = (Integer) v.getTag();
              //  SQLiteDatabase db = context.openOrCreateDatabase("JOB_PORTAL", Context.MODE_PRIVATE, null);
                 id = listName.get(position);
               // listName.remove(position);
                Log.e("Row_keyskill", "position id:  " + id);
                position1=position+1;
                Log.e("Row_keyskill", "position1 id:  "+position1);
                listName.remove(position);
               itemDelete(id);
               //
                 //id = listName.get(position);
                //  db.execSQL("DELETE * FROM USER_SKILLS WHERE U_SKILL_NAME ='" + position + "'");
                 // db.delete("USER_SKILLS", new String("U_SKILL_NAME=?"), new String[]{id});
                  //or some other task*/

                notifyDataSetChanged();
            }
        });
        return convertView;
    }



}


private void itemDelete(String id) {
        //db = null;
        try {
//              db = context.openOrCreateDatabase("JOB_PORTAL", Context.MODE_PRIVATE,null);
          // db.delete("USER_SKILLS", new String("U_SKILL_ID=?"), new String[]{id});
            db.execSQL("DELETE FROM USER_SKILLS WHERE U_SKILL_NAME ='" + id + "'");
        } catch (SQLException se) {
            Toast.makeText(context, "Database error: " + se, Toast.LENGTH_LONG).show();
        }
    }

0 个答案:

没有答案