1

私のアクティビティのレイアウトには、動的に作成した ListView タイプのリストが含まれています (実際の ListView は使用しませんでした)。追加ボタンをクリックすると、EditText と削除ボタンを含む行が作成されます。ユーザーの入力は、EditText から SQLite テーブルに保存されます。削除ボタン以外はすべて機能します。ビューを削除することはできますが、データはテーブルに残ります。削除ボタンをクリックすると、エミュレーターがクラッシュします。

テーブルには、「_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);
}
4

2 に答える 2

1

ContentValues は、削除ではなく、挿入または更新に使用されます。delete 呼び出しでは、次の 3 つのパラメーターを指定します。

  • テーブル名
  • where句
  • where 句の ?s を置き換える 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);

ただし、? を使用するのがベスト プラクティスです。可能な場合はスタイル引数。

于 2013-10-31T14:26:01.350 に答える
0

一見すると、これが問題のある行であると言えます。

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

「値」オブジェクトを渡すのではなく、コメントを渡す必要があります。

于 2013-10-31T14:13:30.513 に答える