如何删除适配器中的列表视图

时间:2014-07-05 02:59:45

标签: android listview adapter

我有一个ListView,我可以删除数据,但如果我再次插入,数据会回来。如何从列表中永久删除数据?

此代码我的活动

public class ListPesanan extends ListActivity implements OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_pemesanan);
        /*
         * Ambil datanya yang ada di database dan serahin ke Adapter biar dia
         * aja yang ngurusin semuanya
         * 
         * Kita percaya aja sama Adapternya :D
         */

        PesananAdapter adapter = new PesananAdapter(this,
                DatabaseTemp.getPesanan(), DatabaseTemp.getJumlah(),
                DatabaseTemp.getBayar());
        setListAdapter(adapter);

        final ListView listView = getListView();
        listView.setOnItemClickListener(this);
        adapter.notifyDataSetChanged();

这个代码我的适配器

@Override
public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;

    if (view == null) {
        holder = new ViewHolder();
        view = layoutInflater.inflate(R.layout.pesan_item, parent, false);

        holder.editTextHarga = (EditText) view.findViewById(R.id.editTextHarga);
        holder.editTextJumlah = (EditText) view.findViewById(R.id.editTextJumlah);
        holder.editTextPesanan = (EditText) view.findViewById(R.id.editTextPesanan);
        holder.Hapus = (Button) view.findViewById(R.id.deletebutton);

        if (view != null) {
            view.setTag(holder);
        }
    } else holder = (ViewHolder) view.getTag();

    holder.editTextHarga.setText(hargaPesanan.get(position));
    holder.editTextJumlah.setText(jumlahPesan.get(position));
    holder.editTextPesanan.setText(pesanan.get(position));

    holder.Hapus.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
        pesanan.remove(position);
        jumlahPesan.remove(position);
        hargaPesanan.remove(position);
        notifyDataSetChanged();
        DatabaseTemp.delete();
        }
    });

    return view;
}

1 个答案:

答案 0 :(得分:0)

你尝试过clear()方法吗?

如果我接受你的代码,它就像是:

DatabaseTemp.clear();