gpt4 book ai didi

java - Listview删除项目和刷新 - android

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:44 25 4
gpt4 key购买 nike

这个问题与此类似 - Android - Listview delete item and Refresh .

我无法刷新我的适配器:

adapter.notifyDataSetChanged();

我试过:

adapter.remove(adapter.getItem(pos));

但没有成功,只有一次(奇怪...)。

还有一个答案:

Call that Activity once again Using Intent

有人可以给我这个(或适配器/游标)的确切代码吗?

我试了几个小时都没有成功。

我的完整代码:

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;
}
});

public void Show_Alert_box(Context context, String message,String time,int position) 最终字符串时间戳=时间;

              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();

最佳答案

你可能已经解决了,但以防万一其他人有同样的问题,这是我的解决方案:

ArrayAdapter<String> myAdapter = (ArrayAdapter<String>)getListView().getAdapter();
myAdapter.remove(myAdapter.getItem(info.position));
myAdapter.notifyDataSetChanged();

问题是您没有列表的适配器。

关于java - Listview删除项目和刷新 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8663853/

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