gpt4 book ai didi

android - 从 ListView 中删除数据

转载 作者:行者123 更新时间:2023-11-29 14:50:10 24 4
gpt4 key购买 nike

我是 Android 开发的新手,我在可能的应用程序中有一个 Listview,Listview 填充了来自 Sqlite 数据库的数据。我想在此 ListView 上使用复选框并删除所选项目。我确实想从 Sqlite Db 中清除选定的数据行。任何有用的帮助将不胜感激。

首先,我在我的应用程序中检索了短信,如下所示:` 如果( bundle != null){

            //—retrieve the SMS message received—
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];

for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
timestamp = smsMessage[n].getTimestampMillis();

number = smsMessage[n].getOriginatingAddress();
body += smsMessage[n].getDisplayMessageBody();
abortBroadcast();
blockMessage(context);
} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);

}
}
private void blockMessage(Context context) {

// instantiate DbMNager object to insert sms in database
//formating receiving time:
//SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d HH:mm:ss a");
String formatedTime = formatter.format(timestamp);
DBmanager= new DbManager(context);
DBmanager.open();
DBmanager.Insert_sms_data(formatedTime ,number,body);
DBmanager.close();`

然后存储在SQlite DB中如下:

public void Insert_sms_data(String formatedTime, String number, String body){
try{

SQLiteDatabase DB = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(SMS_Time, formatedTime);
cv.put(PHONE_NUMBER, number);
cv.put(MESSAGE_BODY, body);
DB.insert(TABLE_SMS, null, cv);
DB.close();

}
catch(Exception ex)
{
Log.e("ERROR in insertion", ex.toString());
}

}

public Cursor Return_All(){

SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery("SELECT * FROM "+"SMS_TABLE", null);
return cur;

}
// Clear all messaged
public void ClearAll(){
SQLiteDatabase db = this .getWritableDatabase();
db.delete(TABLE_SMS, null, null);
db.close();
}

之后在我的 ListView Activity 中成功检索。

public class MainActivity extends Activity {
ListView listViewSMS;
Context context;
DbManager manager;
Button btn_clearall;
Cursor cursor;
SimpleCursorAdapter adapter;
//array from is the column name in your cursor where you're getting the data

String[] from = new String[]{"Phone_number","Message_body","Time"};
//array toIDs contains the id of textViews


int[] toIDs = new int[]{R.id.textViewSMSSender,R.id.textViewMessageBody,R.id.textViewMSMStime};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_activity_main);
context=this;
//get the ListView Reference
try{
listViewSMS=(ListView)findViewById(R.id.listViewSMS);
listViewSMS.setChoiceMode(listViewSMS.CHOICE_MODE_MULTIPLE);

manager = new DbManager(context);
cursor = manager.Return_All();

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview_each_item, cursor, from, toIDs);
listViewSMS.setAdapter(adapter);
adapter.notifyDataSetChanged();
//refreshCursor();

listViewSMS.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {

int itemId = Integer.valueOf(String.valueOf(position));
cursor.moveToPosition(itemId);
int messageId = cursor.getInt(0);
deleteMessage(messageId);

Toast.makeText(getApplicationContext(), "current position"+itemId, Toast.LENGTH_LONG).show();
TextView tv_SMSSenderNumber=(TextView)v.findViewById(R.id.textViewSMSSender);
TextView tv_SMSBody=(TextView)v.findViewById(R.id.textViewMessageBody);
TextView tv_SMSTime=(TextView)v.findViewById(R.id.textViewMSMStime);
String smsSender=tv_SMSSenderNumber.getText().toString();
String smsBody= tv_SMSBody.getText().toString();
String smsTime= tv_SMSTime.getText().toString();
}

});

}

catch(Exception ex){
Log.e("ERROR!!", ex.toString());
}


btn_clearall = (Button)findViewById(R.id.btn_Delall);
btn_clearall.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
clearMessages();
//refreshCursor();

//适配器.notifyDataSetChanged();

        }
});

CheckBox cb_sms_lv = (CheckBox)findViewById(R.id.cb_smslist);


}

protected void clearMessages() {
new AlertDialog.Builder(MainActivity.this).setIcon(
android.R.drawable.ic_dialog_alert).setTitle(R.string.delete)
.setMessage(getString(R.string.clear_message_confirm))
.setPositiveButton(R.string.delete,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int i) {
manager = new DbManager(
context);
manager.open();
manager.ClearAll();
manager.close();
refreshCursor();
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int i) {
}
}).show();
}

private void deleteMessage(final int messageId){
new AlertDialog.Builder(MainActivity.this).setIcon(
android.R.drawable.ic_dialog_alert).setTitle(R.string.delete)
.setMessage(getString(R.string.delete_message_confirm))
.setPositiveButton(R.string.delete,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int i) {
manager = new DbManager(
context);
manager.open();
manager.deleteMessage(messageId);
manager.close();
refreshCursor();
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int i) {
}
}).show();
}

public void refreshCursor() {
manager = new DbManager(context);
manager.open();
cursor = manager.Return_All();

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview_each_item, cursor, from, toIDs);
listViewSMS.setAdapter(adapter);
manager.close();

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

我可以成功删除整个线程,但现在我想使用复选框从 ListView 中删除一些短信请问有什么有用的教程吗?

最佳答案

试试这个

从 ListView 中删除项目的代码

int index = Integer.parseInt(position+"");
ArrayList.remove(index);
notifyDataSetChanged();

然后使用Query从Sqite数据库中删除数据

关于android - 从 ListView 中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com