Добрый день. Я хочу обновить ListView после удаления некоторых элементов из базы данных, но я вижу красные строки под remove
(не может быть разрешен). У меня есть код ниже.ListView обновить после удаления некоторых предметов
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listdisplay1);
dbHelper = new MyDatabaseHelper(this);
sqlcon = new InfoAPI(this);
final String name1 = getIntent().getExtras().getString("name");
BuildList(name1);
}
public void BuildList(String name) {
final String name1 = name;
sqlcon.open();
Cursor cursor=sqlcon.readEntry(name1);
String[] columns=new String[]{
MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
};
int[] to=new int[]{
R.id.weather,R.id.date,R.id.status,R.id.in,R.id.out
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.listdispaly,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String ID =
cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String date1 = cursor.getString(cursor.getColumnIndexOrThrow("Date"));
Intent intent = new Intent(ListDisplay.this, UpdatePage.class);
intent.putExtra("name1", name1);
intent.putExtra("date1", date1);
intent.putExtra("ID", ID);
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
// final long id1=id;
AlertDialog.Builder builder = new AlertDialog.Builder(ListDisplay.this);
builder.setTitle("Delete");
builder.setMessage("Are you sure you want to delete?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
database = dbHelper.getWritableDatabase();
Cursor cursor = (Cursor) p.getItemAtPosition(po);
// Get the state's capital from this row in the database.
long ID =
cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
sqlcon.delete(ID);
p.remove(p.getItemAtPosition(po));
dataAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int ii) {
dialog.dismiss();
}
}
);
builder.show();
return true;
}
});
}
}
Может кто-нибудь помочь мне разобраться с проблемой? Я новичок в андроид и не уверен, что это правильный способ реализации? Спасибо
Edited
Итак, я сделал то, что, как предложил @Sanju и теперь получаю эту ошибку
10-19 04:47:48.543 2094-2094/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 2094
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.project.project.ListDisplay
at com.example.project.project.ListDisplay$2$1.onClick(ListDisplay.java:109)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
at android.os.Handler.dispatchMessage(Handler.java:102)
Код отредактирован
database = dbHelper.getWritableDatabase();
Cursor cursor = (Cursor) p.getItemAtPosition(po);
// Get the state's capital from this row in the database.
long ID =
cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
sqlcon.delete(ID);
((ListDisplay)getApplicationContext()).finish();
Intent intent = new Intent(getApplicationContext(), ListDisplay.class);
getApplicationContext().startActivity(intent);
}
});
Любая идея?
Последние
final
добавлен в columns
, to
и listView
.
final String[] columns=new String[]{
MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
};
final int[] to=new int[]{
R.id.weather,R.id.date,R.id.status,R.id.in,R.id.out
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.listdispaly,
cursor,
columns,
to,
0);
final ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
И сделал как этот
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
// final long id1=id;
AlertDialog.Builder builder = new AlertDialog.Builder(ListDisplay.this);
builder.setTitle("Delete");
builder.setMessage("Are you sure you want to delete?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
database = dbHelper.getWritableDatabase();
Cursor cursor = (Cursor) p.getItemAtPosition(po);
// Get the state's capital from this row in the database.
long ID =
cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
sqlcon.delete(ID);
}
});
Cursor cursor=sqlcon.readEntry(name1);
dataAdapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.listdispaly,
cursor,
columns,
to, 0);
//listView.setAdapter(null);
listView.setAdapter(dataAdapter);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int ii) {
dialog.dismiss();
}
}
);
builder.show();
return true;
}
});
Все кажется отлично. Но когда я долгое время нажимаю строку и выворачивается AlertDialog
, весь текст становится белым. Почему это произойдет?
Решена ли ваша проблема? –
@AnsalAli no ... – Hoo
Любой может помочь? – Hoo