この質問はこれに似ています - Android - Listview delete item and Refresh。リストビュー削除アイテムとリフレッシュ - アンドロイド
私は私のアダプターをリフレッシュカント:
adapter.notifyDataSetChanged();
私が試した:
adapter.remove(adapter.getItem(pos));
が、成功せず、一度だけ(変なの...)。
別の答えはそこにある:
Call that Activity once again Using Intent
sombodyは、私は、このための正確なコードを与える(またはアダプタ/カーソルのために)ことができますか?
私はこれを数時間試して成功しています。
私の完全なコード:
protected void onCreate (Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.personalmessageview);
headtitle= getIntent().getExtras().getString("head");
setTitle(headtitle);
personalresults = getIntent().getExtras().getStringArrayList("personalres");
personalresultswithtime = getIntent().getExtras().getStringArrayList("personalrestime");
// setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,personalresults));
ListView list = (ListView)findViewById(R.id.listview_personal);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, personalresults);
list.setAdapter(adapter);
registerForContextMenu(list);
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
String time = personalresultswithtime.get(pos).toString();
Show_Alert_box(v.getContext(),"Please select action.",time,pos);
return true;
}
});
公共ボイドShow_Alert_box(コンテキスト・コンテキスト、文字列メッセージ、文字列の時間、INT位置) 最終列タイムスタンプ=時間。
final int pos = position;
final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(getString(R.string.app_name));
alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try
{
db = databaseHelper.getWritableDatabase();
db.delete("messages","timestamp" + "=?", new String[] { timestamp });
Log.d("DB"," delete! ");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(PersonalMessageView.this, android.R.layout.simple_list_item_1, personalresults);
adapter.remove(adapter.getItem(pos)); //not working t all! why ?
list.notify();
list.invalidate();
personalresults.remove(pos);
personalresultswithtime.remove(pos);
adapter.notifyDataSetChanged();
db.close();
}
catch(Exception e)
{
}
} });
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
} });
alertDialog.setMessage(message);
alertDialog.show();
}
は、アダプタから項目を削除してから日付のセットを通知してください、あなたのアダプタのために何を使用している –
をchaged?つまり、ArrayAdapterまたはBaseAdapter、またはそれらのいずれかから派生したクラスですか? –
iv'eがコードを追加しました。もう一度それを確認してください。 – Smoker09