从Android SQLite数据库中删除动态创建的EditText

问题描述:

我的活动布局包含一个我动态创建的ListView类型列表(我没有使用实际的ListView)。单击一个添加按钮,它将创建一个包含EditText和一个删除按钮的行。用户的输入从EditText保存到SQLite表中。除了删除按钮,一切都可以使用。我可以删除视图,但数据仍保留在表格中。点击删除按钮后,仿真器崩溃。从Android SQLite数据库中删除动态创建的EditText

该表只包含一个“_Id”列和“注释”列。我是Android开发人员的初学者,但对SQLite缺乏经验;这是我的第一次尝试。 对不起,如果我添加了太多的代码。我添加了整个数据源类,但感觉我要过度了。如果你想看更多,只需告诉我,因为我永远不知道我所附的所有东西是否有用。

这里是我的方法,将行的EditText输入保存到数据库。

public Comment createComment(String comment) { 
    ContentValues values = new ContentValues(); 
    values.put(SQLiteHelper.COLUMN_COMMENT, comment); 
    long insertId = database.insert(SQLiteHelper.TABLE_COMMENTS, null, 
      values); 
    Cursor cursor = database.query(SQLiteHelper.TABLE_COMMENTS, 
      allColumns, SQLiteHelper.COLUMN_ID + " = " + insertId, null, 
      null, null, null); 
    cursor.moveToFirst(); 
    Comment newComment = cursorToComment(cursor); 
    cursor.close(); 
    return newComment; 
} 

这里是我尝试从数据库中删除一行数据。

public void deleteComment(String comment) { 
    ContentValues values = new ContentValues(); 
    values.put(SQLiteHelper.COLUMN_COMMENT, comment); 

    database.delete(SQLiteHelper.TABLE_COMMENTS, SQLiteHelper.COLUMN_COMMENT 
      + " = " + values, null); 

} 

这是我的评论类。考虑到注释只是EditText中的一个字符串,不确定是否有必要。我从一个在线资源中借了很多代码,所以我不完全清楚它应该如何组合在一起。

public class Comment { 
private long id; 
private String comment; 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 

public String getComment() { 
    return comment; 
} 

public void setComment(String comment) { 
    this.comment = comment; 
} 

// Will be used by the ArrayAdapter in the ListView 
@Override 
public String toString() { 
    return comment; 
} 

}

下面是从我的主要活动一些代码,动态创建的意见。

// onClick handler for the "Add new" button; 
public void onAddNewClickedStrengths(View v) { 
    // Inflate a new row and hide the button self. 

    inflateEditRowStrengths(null); 


    v.setVisibility(View.GONE); 
} 



// Helper for inflating a row 
private void inflateEditRowStrengths(String name) { 

    idCount++; 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View rowView = inflater.inflate(R.layout.list_row, null); 
    final ImageButton deleteButton = (ImageButton) rowView 
      .findViewById(R.id.button); 
    final EditText editText = (EditText) rowView 
      .findViewById(R.id.editText); 
    editText.setId(idCount); 


    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus) { 

       comment = datasource.createComment(editText.getText().toString()); 

      } 

     } 
    }); 


    load.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      datasource = new CommentsDataSource(getBaseContext()); 
      datasource.open(); 

      List<Comment> values = datasource.getAllComments(); 

      for (int i = 1; i < values.size(); i++){ 
       inflateEditRowStrengths(values.get(i).toString()); 

      } 



     } 
    }); 


    clear.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      datasource.deleteAllComments(); 

      List<Comment> values = datasource.getAllComments(); 

      for (int i = 0; i < values.size(); i++){ 
       inflateEditRowStrengths(values.get(i).toString()); 

      } 
     } 
    }); 



    deleteButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String toFind = editText.getText().toString(); 

      datasource.deleteComment(toFind); 
      mContainerViewStrengths.removeView(rowView); 



     } 
    }); 



    if (name != null && !name.isEmpty()) { 
     editText.setText(name); 
    } else { 
     mExclusiveEmptyView = rowView; 
     deleteButton.setVisibility(View.INVISIBLE); 
    } 

    // A TextWatcher to control the visibility of the "Add new" button and 
    // handle the exclusive empty view. 
    editText.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable s) { 

      if (s.toString().isEmpty()) { 
       mAddButtonStrengths.setVisibility(View.GONE); 
       deleteButton.setVisibility(View.INVISIBLE); 

       if (mExclusiveEmptyView != null 
         && mExclusiveEmptyView != rowView) { 
        mContainerViewStrengths.removeView(mExclusiveEmptyView); 
        editText.getText(); 
       } 
       mExclusiveEmptyView = rowView; 
      } else { 

       if (mExclusiveEmptyView == rowView) { 
        mExclusiveEmptyView = null; 
       } 

       mAddButtonStrengths.setVisibility(View.VISIBLE); 
       deleteButton.setVisibility(View.VISIBLE); 

      } 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
             int after) { 
     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
            int count) { 
     } 
    }); 

    // Inflate at the end of all rows but before the "Add new" button 


    mContainerViewStrengths.addView(rowView, mContainerViewStrengths.getChildCount() - 1); 
} 

ContentValues用于插入或更新,不用于删除。对于删除调用你提供这三个参数:?

  • 表名
  • where子句
  • 的地方,其更换。你where子句中子句参数

所以你需要:

String where = SQLiteHelper.COLUMN_COMMENT + " = ? "; 
String[] whereArgs = new String[] {comment}; 
database.delete(SQLiteHelper.TABLE_COMMENTS, where, whereArgs); 

由于SQL,在内部编译如下:

DELETE FROM COMMENTS WHERE comment = 'the value of comment' 

请注意,您可以直接把参数在where子句中,如果你想这样的:

String where = SQLiteHelper.COLUMN_COMMENT + " = '" + comment + "'"; 
database.delete(SQLiteHelper.TABLE_COMMENTS, where, null); 

然而,它是用最好的做法是什么?尽可能的风格参数。

+0

非常感谢,这对我来说。 – domi91c

乍一看,我会说,这是有问题的行:

database.delete(SQLiteHelper.TABLE_COMMENTS, SQLiteHelper.COLUMN_COMMENT 
      + " = " + values, null); 

你不应该通过“值”的对象,而不是通过评论。