此代码用于删除和显示数据,我尝试删除并在删除数据后我希望listview刷新。我有这个代码List.java:
package id.co.pips.databasecrud;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import id.co.pips.databasecrud.PDM.Mahasiswa;
import static id.co.pips.databasecrud.DbHelper.FIELD_MAHASISWA_NIM;
import static id.co.pips.databasecrud.R.id.nimItem;
import static id.co.pips.databasecrud.R.id.view;
public class ListNimNama extends AppCompatActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_nim_nama);
lv = (ListView) findViewById(R.id.listView);
DbHelper db = new DbHelper(this);
Cursor cursor = db.getAllNamaNimMahasiswa();
String[] fromColumns = {DbHelper.FIELD_MAHASISWA_NIM, DbHelper.FIELD_MAHASISWA_NAMA};
int[] toViews = {R.id.nimItem, R.id.namaItem};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_list_nim_nama, cursor, fromColumns, toViews, 0);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
TextView nim = (TextView) view.findViewById(R.id.nimItem);
TextView nama = (TextView) view.findViewById(R.id.namaItem);
String vnim = nim.getText().toString();
String vnama = nama.getText().toString();
System.out.println("Nama = : " + vnama);
System.out.println("NIM = : " + vnim);
Intent app = new Intent(ListNimNama.this, editActivity.class);
app.putExtra("nim", vnim);
app.putExtra("nama", vnama);
startActivity(app);
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
// TODO: Get the position of the item clicked.
// Delete it from your collection eg.ArrayList.
// Call notifydatasetChanged so that it will refresh
// the views displaying updated list.
AlertDialog.Builder alert = new AlertDialog.Builder(ListNimNama.this);
alert.setTitle("Alert!!");
alert.setMessage("Are you sure to delete this item ?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do your work here
delete();
dialog.dismiss();
}
});
alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
return true;
}
});
}
public void delete()
{
DbHelper dbAdmin;
try
{
TextView nim = (TextView)findViewById(R.id.nimItem);
String vnim = nim.getText().toString();
dbAdmin = new DbHelper(this);
dbAdmin.deleteMahasiswa(vnim);
Toast.makeText(this, "Berhasil Delete Data " , Toast.LENGTH_LONG).show();
}catch (SQLException e)
{
Toast.makeText(this, "Gagal Delete Data " , Toast.LENGTH_LONG).show();
}
}
}
已经放
adapter.notifyDataSetChanged();
List.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_list_nim_nama"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="id.co.pips.databasecrud.ListNimNama">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nimItem"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/namaItem"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
但删除数据后没有任何变化。或者我错了代码?
答案 0 :(得分:0)
您应该在更新数据结构后调用adapter.notifyDataSetChanged();
。
试试这个:
public void delete()
{
DbHelper dbAdmin;
try
{
TextView nim = (TextView)findViewById(R.id.nimItem);
String vnim = nim.getText().toString();
dbAdmin = new DbHelper(this);
dbAdmin.deleteMahasiswa(vnim);
Toast.makeText(this, "Berhasil Delete Data " , Toast.LENGTH_LONG).show();
}catch (SQLException e)
{
Toast.makeText(this, "Gagal Delete Data " , Toast.LENGTH_LONG).show();
}
adapter.notifyDataSetChanged(); // <= Add this
}