0

我想从单击按钮删除行,当我删除数字“1231312”时,行已被删除,但如果删除数字“08982312”行不删除。每个数字都从 0 开始,行不能删除,不能强制关闭但不能删除。这是我的列表代码:

黑名单提供者.java

公共类 BlackListProvider 扩展 ContentProvider { public static final String PROVIDER_NAME = "skripsi.ebranda.provider.blacklist";

public static final Uri CONTENT_URI = Uri.parse("content://"
        + PROVIDER_NAME + "/blacklist");

public static final String _ID = "_id";
public static final String Number = "number";

static final int BLACKLIST = 1;
static final int BLACKLIST_ID = 2;

static final UriMatcher uriMatcher;
static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(PROVIDER_NAME, "blacklist", BLACKLIST);
    uriMatcher.addURI(PROVIDER_NAME, "blacklist/#", BLACKLIST_ID);
}

// ---for database use---
static SQLiteDatabase blacklistDB;
private static final String DATABASE_NAME = "BlackList";
static final String DATABASE_TABLE = "blacklist";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
        + DATABASE_TABLE + " (_id integer primary key autoincrement, "
        + " number text not null);";

public static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("Content provider database",
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}

@Override
public String getType(Uri uri) {
    switch (uriMatcher.match(uri)) {
    // ---get all books---
    case BLACKLIST:
        return "vnd.android.cursor.dir/vnd.com.blacklist ";
        // ---get a particular book---
    case BLACKLIST_ID:
        return "vnd.android.cursor.item/vnd.com.blacklist ";
    default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
}

@Override
public boolean onCreate() {
    Context context = getContext();
    DatabaseHelper dbHelper = new DatabaseHelper(context);
    blacklistDB = dbHelper.getWritableDatabase();
    return (blacklistDB == null) ? false : true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
    sqlBuilder.setTables(DATABASE_TABLE);

    if (uriMatcher.match(uri) == BLACKLIST_ID)
        // ---if getting a particular book---
        sqlBuilder.appendWhere(_ID + " = " + uri.getPathSegments().get(1));

    if (sortOrder == null || sortOrder == "")
        sortOrder = _ID;

    Cursor c = sqlBuilder.query(blacklistDB, projection, selection,
            selectionArgs, null, null, sortOrder);

    // ---register to watch a content URI for changes---
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // ---add a new book---
    long rowID = blacklistDB.insert(DATABASE_TABLE, "", values);

    // ---if added successfully---
    if (rowID > 0) {
        Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
        getContext().getContentResolver().notifyChange(_uri, null);
        return _uri;
    }
    throw new SQLException("Failed to insert row into " + uri);
}

@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
    // arg0 = uri
    // arg1 = selection
    // arg2 = selectionArgs
    int count = 0;
    switch (uriMatcher.match(arg0)) {
    case BLACKLIST:
        count = blacklistDB.delete(DATABASE_TABLE, arg1, arg2);
        break;
    case BLACKLIST_ID:
        String id = arg0.getPathSegments().get(1);
        count = blacklistDB.delete(DATABASE_TABLE, _ID + " = " + id
                + (!TextUtils.isEmpty(arg1) ? " AND (" + arg1 + ')' : ""),
                arg2);
        break;
    default:
        throw new IllegalArgumentException("Unknown URI " + arg0);
    }
    getContext().getContentResolver().notifyChange(arg0, null);
    return count;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    int count = 0;
    switch (uriMatcher.match(uri)) {
    case BLACKLIST:
        count = blacklistDB.update(DATABASE_TABLE, values, selection,
                selectionArgs);
        break;
    case BLACKLIST_ID:
        count = blacklistDB.update(
                DATABASE_TABLE,
                values,
                _ID
                        + " = "
                        + uri.getPathSegments().get(1)
                        + (!TextUtils.isEmpty(selection) ? " AND ("
                                + selection + ')' : ""), selectionArgs);
        break;
    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

黑名单.java

公共类 BlackList 扩展 ListActivity {

private ArrayList<String> numberList;
private Cursor cursor = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    ListAdapter adapter = createBlackListAdapter();
    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the textview text
            // CharSequence content = ((TextView)view).getText();
            TextView t1 = (TextView) view.findViewById(R.id.tulisan);
            String address = t1.getText().toString();
            popUp(address);
        }
    });

}
public void popUp(String address) {
    Bundle bundle = new Bundle();
    bundle.putString("address", address);
    bundle.putString("deletebutton", "Delete");
    bundle.putString("closebutton", "Close");

    Intent newIntent = new Intent(this.getApplicationContext(),
            ListNumberBlack.class);
    newIntent.putExtras(bundle);
    startActivityForResult(newIntent, 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    new MenuInflater(this).inflate(R.menu.menu, menu);
    return(super.onCreateOptionsMenu(menu));
}
public boolean onOptionsItemSelected(MenuItem item){
    if (item.getItemId()==R.id.addnumbermenu){
        startActivity(new Intent(BlackList.this,AddBlackList.class));
        return (true);
            }
    return (super.onOptionsItemSelected(item));
}



protected ListAdapter createBlackListAdapter() {
    try {
        numberList = new ArrayList<String>();

        cursor = getContentResolver().query(
                                BlackListProvider.CONTENT_URI,
                                null,
                                null, 
                                null,
                                null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                String number = cursor.getString(cursor
                        .getColumnIndex("number"));

                numberList.add(number);

            } while (cursor.moveToNext());

        }
    } catch (Exception e) {
        Log.e("BACKGROUND_PROC", e.getMessage());
    }
    return new ArrayAdapter<String>(this,
                    R.layout.list_item,numberList);

}

}

列表编号黑色

公共类 ListNumberBlack 扩展 Activity {

private String address;
protected static final String ID_EXTRA = "skripsi.ebranda.nomorespam._ID";
long id;
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_number);
    Bundle bundle = this.getIntent().getExtras();
    address = bundle.getString("address");
    //id = bundle.getInt(ID_EXTRA);
    //String X = String.valueOf(id);
    final String s = String.valueOf(address);
    Toast.makeText(ListNumberBlack.this,
            s, Toast.LENGTH_SHORT)
            .show();

    // set text
    TextView txtContent = (TextView) findViewById(R.id.ListNumber);
    txtContent.setText(address);
    // --set button
    // cancel button
    String name = txtContent.getText().toString();

    Button cancelButton = (Button) findViewById(R.id.cancel_button);
    cancelButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            finish();
        }
    });

    Button deleteButton = (Button) findViewById(R.id.delete_button);
    deleteButton.setOnClickListener(new OnClickListener(){
    //@Override
    public void onClick(View v) {
                // hapus pesan
        Dialog.showConfirmation(ListNumberBlack.this,
                R.string.hapus2,
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,
                            int which) {
                        BlackListProvider.blacklistDB.delete(BlackListProvider.DATABASE_TABLE, BlackListProvider.Number
                                + "=" + s, null);
                                finish();
                                Toast.makeText(ListNumberBlack.this,
                                        "Nomor Terhapus", Toast.LENGTH_SHORT)
                                        .show();
            }

        });

} });

4

1 回答 1

0

我不确定确切的答案,但我有一些您可能想尝试的想法:

1-你为什么有final String s = String.valueOf(address);?为什么不只是final String s = address;

2-尝试在 中添加单引号String s,如下所示:

BlackListProvider.blacklistDB.delete(BlackListProvider.DATABASE_TABLE, 
        BlackListProvider.Number + "='" + s +"'", null);

让我知道这是否有帮助。

于 2013-06-09T08:09:52.377 回答