从数据库中删除项目后,我无法刷新ListView。我的应用程序允许您在列表中选择一个条目,该列表将打开一个新活动并根据您单击的项目显示信息(我传递列表项ID)并且该活动中有一个删除按钮。删除时,选定项之前的条目将从ListView中删除,并且在选择替换项时信息为空。
有人能给我一些关于发生了什么的见解吗?每个人都建议使用notifySetDataChanged()
,但这对我不起作用。
以下是MainActivity:
package com.example.msdproject;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.content.Intent;
import android.widget.ListView;
import android.widget.Button;
import android.widget.ArrayAdapter;
import android.view.View;
import java.sql.SQLException;
import java.util.ArrayList;
import android.util.Log;
public class MainActivity extends ListActivity {
public ArrayList<String> data_list=new ArrayList<String>();
DBManager db = new DBManager(this);
public ArrayAdapter<String> concertList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add = (Button)findViewById(R.id.addConcertButton);
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AddArtist.class);
startActivity(i);
}
});
try {
db.open();
} catch (SQLException e){
e.printStackTrace();
}
addData();
//db.close();
}
@Override
public void onResume()
{
super.onResume();
addData();
}
public void addData()
{
data_list.clear();
ListView listView = (ListView) findViewById(android.R.id.list);
Cursor c = db.getAllConcerts();
if (c.moveToFirst())
{
do {
data_list.add(c.getString(0));
} while (c.moveToNext());
}
concertList = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, data_list);
listView.setAdapter(concertList);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(MainActivity.this, ViewArtist.class);
i.putExtra("id", id);
startActivity(i);
}
}
以下是ViewArtist活动:
package com.example.msdproject;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.SQLException;
public class ViewArtist extends Activity
{
DBManager db = new DBManager(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_artist);
Intent intent = getIntent();
final long num = intent.getLongExtra("id", 1);
Button update = (Button)findViewById(R.id.updateConcertButton);
update.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(ViewArtist.this, UpdateArtist.class);
i.putExtra("id", num);
startActivity(i);
}
});
String name;
String venue;
String date;
String comments;
try {
db.open();
Cursor c = db.getConcert(num + 1);
//The reason I have num + 1 is because List Item 1 has a ROW_ID of 0
if (c.moveToFirst())
{
do
{
name = (c.getString(1));
venue = (c.getString(2));
date = (c.getString(3));
comments = (c.getString(4));
TextView nameTxt = (TextView)findViewById(R.id.viewName);
TextView titleTxt = (TextView)findViewById(R.id.viewTitle);
TextView venueTxt = (TextView)findViewById(R.id.viewVenue);
TextView dateTxt = (TextView)findViewById(R.id.viewDate);
TextView commentsTxt = (TextView)findViewById(R.id.viewComments);
nameTxt.setText(" " + name);
titleTxt.setText("ROW_ID: " + String.valueOf(num));
venueTxt.setText(" " + venue);
dateTxt.setText(" " + date);
commentsTxt.setText(" " + comments);
Button del = (Button)findViewById(R.id.deleteConcertButton);
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.deleteConcert(num);
Toast.makeText(ViewArtist.this, "Concert Deleted", Toast.LENGTH_LONG).show();
}
});
} while (c.moveToNext());
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
这是包含删除数据库条目的代码的DBManager类:
package com.example.msdproject;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import java.sql.SQLException;
public class DBManager {
public static final String COL_ROWID = "_id";
public static final String COL_NAME = "name";
public static final String COL_VENUE = "venue";
public static final String COL_DATE = "date";
public static final String COL_COMMENTS = "comments";
private static final String DB_NAME = "Concerts";
private static final String DB_TABLE = "Concert_Info";
private static final int DB_VERSION = 1;
private static final String DB_CREATE =
"create table " + DB_TABLE +
" (_id integer primary key autoincrement, " +
"name text not null, " +
"venue text not null, " +
"comments text not null, " +
"date text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBManager(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
//embedded class
public static class DatabaseHelper extends SQLiteOpenHelper
{
//context refers to activity that called it
DatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
/*
//taken from YouTube Tutorial video
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("drop table if exists Concert_Info");
onCreate(db);*/
}
}
public DBManager open() throws SQLException
{
db = DBHelper.getWritableDatabase();
//db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE + ";");
return this;
}
public void close()
{
DBHelper.close();
}
public long getId(String name)
{
long x = 10;
return x;
}
public long insertConcert(String name, String venue, String date, String comments)
{
ContentValues initialValues = new ContentValues();
initialValues.put(COL_NAME, name);
initialValues.put(COL_VENUE, venue);
initialValues.put(COL_DATE, date);
initialValues.put(COL_COMMENTS, comments);
return db.insert(DB_TABLE, null, initialValues);
}
public boolean deleteConcert(long ROW_ID)
{
return db.delete(DB_TABLE, COL_ROWID + "=" + ROW_ID, null) > 0;
}
public Cursor getAllConcerts() {
return db.query(DB_TABLE, new String[]
{
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
null,
null,
null,
null,
null
);
}
public Cursor getConcert(long ROW_ID) throws SQLException
{
Cursor mCursor =
db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
COL_ROWID + "=" + ROW_ID,
null,
null,
null,
null
);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateConcert(String name, String venue, String date, String comments)
{
ContentValues updateValues = new ContentValues();
updateValues.put(COL_NAME, name);
updateValues.put(COL_VENUE, venue);
updateValues.put(COL_DATE, date);
updateValues.put(COL_COMMENTS, comments);
return db.update(DB_TABLE, updateValues, COL_NAME + "=" + name, null) > 0;
}
public void reset () throws SQLException {
db.delete(DB_TABLE, null, null);
db.close();
this.DBHelper.onCreate(this.db);
}
}
答案 0 :(得分:0)
将ListView与数据库中的一组数据同步可能非常棘手。 基本上,每当您知道数据库中的数据已被修改时,您需要重新查询它们,使用新数据更新适配器,最后调用onDataSetChanged()。
我建议您使用CursorAdapter,这是一个自适应填充列表的适配器,其结果来自Cursor(例如SQLiteCursor)。
答案 1 :(得分:0)
因为您正在查询COL_ROWID =&#34; _id&#34;,
db.delete(DB_TABLE, COL_ROWID + "=" + ROW_ID, null) > 0;
是,
_id integer primary key autoincrement,
因此,一旦删除了行,它就显示没有数据(但是_id存在)。
你可以做的是,在你的onListItemClick中,通过查询字符串找到_id并传递它。