Java 删除某些项目后刷新ListView
大家好。我想在从数据库中删除一些项目后刷新ListView,但我看到Java 删除某些项目后刷新ListView,java,android,sqlite,listview,Java,Android,Sqlite,Listview,大家好。我想在从数据库中删除一些项目后刷新ListView,但我看到删除下面有一条红线(无法解决)。我有下面的代码 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listdisplay1); dbHelper = new MyDatabaseHelper(this);
删除
下面有一条红线(无法解决)。我有下面的代码
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;
}
});
}
}
已编辑代码
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 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;
}
});
listView.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
长点击(最终适配器视图p、视图v、最终整数po、长id){
//最终长id1=id;
AlertDialog.Builder=新建AlertDialog.Builder(ListDisplay.this);
建造商名称(“删除”);
setMessage(“您确定要删除吗?”);
builder.setIcon(android.R.drawable.ic\u对话框\u警报);
builder.setPositiveButton(“是”,新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,int ii){
database=dbHelper.getWritableDatabase();
Cursor Cursor=(Cursor)p.getItemAtPosition(po);
//从数据库中的此行获取州的资本。
长ID=
cursor.getLong(cursor.getColumnIndexOrThrow(“\u id”);
sqlcon.delete(ID);
}
});
Cursor Cursor=sqlcon.readEntry(name1);
dataAdapter=新的SimpleCorsorAdapter(
getApplicationContext(),R.layout.listdispaly,
光标,
柱,
至(0);
//setAdapter(空);
setAdapter(dataAdapter);
setNegativeButton(“取消”,新建DialogInterface.OnClickListener())
{
公共void onClick(对话框接口对话框,int ii){
dialog.dismise();
}
}
);
builder.show();
返回true;
}
});
一切似乎都很好。但是,当我长按该行并弹出
AlertDialog
时,所有文本都变为白色。为什么会发生这种情况?这就是我删除列表行的方法尝试在适配器类中执行类似操作
delet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cartdata.updateSelected(Integer.parseInt(cart_pdiscription[i]), 0);
cartdata.deleteProduct(Integer.parseInt(cart_pdiscription[i]));
Intent intent = ((CartList) context).getIntent();
((CartList) context).finish();
context.startActivity(intent);
}
});
这就是我所做的删除列表行的操作,请在适配器类中尝试类似的操作
delet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cartdata.updateSelected(Integer.parseInt(cart_pdiscription[i]), 0);
cartdata.deleteProduct(Integer.parseInt(cart_pdiscription[i]));
Intent intent = ((CartList) context).getIntent();
((CartList) context).finish();
context.startActivity(intent);
}
});
使用此(非编译)行,您将尝试操作列表视图:
p.remove(p.getItemAtPosition(po));
这不是它的工作原理。不要试图直接操纵视图。使用适配器为视图提供数据的想法是,您操作数据,而不是视图,然后发出有变化的信号。诀窍在于知道如何发出信号。似乎你的下一行就是这样做的:
dataAdapter.notifyDataSetChanged();
因此,删除第一行(使用p.remove),它实际上可能会工作
如果这还不够(尽管我认为应该是),那么您可能需要将适配器中的光标换成新的光标,如下所示:
Cursor cursor2 = ... // create the same way you did when you created SimpleCursorAdapter
dataAdapter.changeCursor(cursor2);
在更新的代码中:
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 cursor2 = sqlcon.readEntry(name1);
dataAdapter.changeCursor(cursor2);
}
});
使用此(非编译)行,您将尝试操作列表视图:
p.remove(p.getItemAtPosition(po));
这不是它的工作原理。不要试图直接操纵视图。使用适配器为视图提供数据的想法是,您操作数据,而不是视图,然后发出有变化的信号。诀窍在于知道如何发出信号。似乎你的下一行就是这样做的:
dataAdapter.notifyDataSetChanged();
因此,删除第一行(使用p.remove),它实际上可能会工作
如果这还不够(尽管我认为应该是),那么您可能需要将适配器中的光标换成新的光标,如下所示:
Cursor cursor2 = ... // create the same way you did when you created SimpleCursorAdapter
dataAdapter.changeCursor(cursor2);
在更新的代码中:
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 cursor2 = sqlcon.readEntry(name1);
dataAdapter.changeCursor(cursor2);
}
});
我看到下面有一条红线(无法解决)
这是因为类AdapterView
没有名为remove
的方法。
也请参考这些
执行代码以删除编辑中选定Id的行
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);
}
});
and then try this snippet
cursor=sqlcon.readEntry(name1);
dataAdapter = new SimpleCursorAdapter(
this, R.layout.listdispaly,
cursor,
columns,
to, 0);
//listView.setAdapter(null);
listView.setAdapter(dataAdapter);
但当我长按该行并弹出AlertDialog时,所有文本都变成白色。为什么会发生这种情况
我不知道为什么会发生这种情况,但是这个片段可以帮助你解决这个问题
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
它将强制背景色反转。此标志仅用于预材料主题。此方法在API级别23中被弃用。此标志仅用于材质前主题。相反,请在警报对话框主题上使用指定窗口背景。
我看到下面有一条红线(无法解决)
这是因为类AdapterView
没有名为remove
的方法。
也请参考这些
执行代码以删除编辑中选定Id的行
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);
}
});
and then try this snippet
cursor=sqlcon.readEntry(name1);
dataAdapter = new SimpleCursorAdapter(
this, R.layout.listdispaly,
cursor,
columns,
to, 0);
//listView.setAdapter(null);
listView.setAdapter(dataAdapter);
但当我长按该行并弹出AlertDialog时,所有文本都变成白色。为什么会发生这种情况
我不知道为什么会发生这种情况,但是这个片段可以帮助你解决这个问题
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
它将强制背景色反转。此标志仅用于预材料主题。
此方法在API级别23中被弃用。此标志仅用于材质前主题。相反,请在警报对话框主题上使用指定窗口背景。
无法解析CartList?上下文,我将更改为getApplicationContext。但是还是不走运..你从哪里得到CartList?无法解析上下文,我改为getApplicationContext。但是仍然没有运气。fetchAllRoutines中应该有什么?用创建SimpleCorsorAdapter
时的方法创建它。(更新了我的帖子)在sqlcon.delete()之后创建?不,就像您在BuildList
中使用sqlcon.readEntry(name1)
所做的那样。你需要重新创建相同的查询。是的,我知道,但是