【问题标题】:How can I delete and refresh my listview?如何删除和刷新我的列表视图?
【发布时间】:2015-02-28 13:13:07
【问题描述】:

我创建了一个方法 deleteItem 必须删除该行,但是在我单击按钮后它会继续存在,如果我再试一次,我会遇到异常。

public class WITHistory extends WITBase implements OnItemClickListener{
private ListView listView;
public int del;
@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history_wit);

    listView = (ListView) findViewById(R.id.listSearch);
    HistoryAdapter adapter = new HistoryAdapter(this, searches);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(this, "You selected Row ["+pos+"]\n"+
"For donation data ["+searches.get(pos)+"]\n"+"With ID of ["+id+"]", Toast.LENGTH_LONG).show();
    del = pos;

}

public void deleteItem(View v) {
    searches.remove(del);

}

}

【问题讨论】:

  • 你必须用 adapter.notifyDataSetChanged() 刷新适配器,注意适配器必须像你声明 int del 一样全局声明
  • 还有搜索来自哪里?

标签: android listview refresh


【解决方案1】:

将您的适配器声明为类成员

并添加

public void deleteItem(View v) {
    searches.remove(del);
adapter.notifyDataSetChanged ().;

}

【讨论】:

  • 谢谢伊什拉特。我之前尝试过使用 adapter.notifyDataSetChanged();我没有成功,因为我从未将我的适配器声明为类成员:)
  • 你为什么不全局声明你的适配器?
猜你喜欢
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多