我有一个填充了本地存储数据库的ListView。当您单击列表项时,它会将您带到viewArtist活动,该活动根据您单击的项目的ROW_ID填充带有行条目信息的textView框。这是使用putExtra()完成的。 viewArtist活动有一个删除条目的按钮。当您删除某个条目时,该列表不会刷新,因此您必须关闭该应用并重新登录。
但我的主要问题是删除某个项目后,该列表不再显示该条目,但是当您点击该项目代替已删除的项目时,下一个editText视图中不会显示任何信息屏幕。除此之外,当您单击此列表后面的任何列表条目时,它会使用上一个列表条目的信息填充textViews。
这是一张图片,用于说明我试图解释的内容。
图片1:http://i.imgur.com/RsXyygF.jpg
好像删除了行,但列表仍然将每个ROW_ID与列表项匹配,并且没有自动增量。
任何帮助都会非常感激,因为我已经试图解决这个问题了好几天! 提前谢谢!
以下是调用viewArtist类的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;
public class MainActivity extends ListActivity {
public ArrayList<String> data_list=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.addConcertButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AddArtist.class);
startActivity(i);
}
});
DBManager db = new DBManager(this);
try {
db.open();
} catch (SQLException e) {
e.printStackTrace();
}
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());
}
ArrayAdapter<String> concertList=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data_list);
listView.setAdapter(concertList);
concertList.notifyDataSetChanged();
db.close();
}
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);
String strLong = Long.toString(id);
//String idString = Objects.toString(id, null);
if(strLong == "null")
{
id = id + 1;
i.putExtra("id", id);
startActivity(i);
}
else
{
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 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 btn = (Button)findViewById(R.id.updateConcertButton);
btn.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 btn2 = (Button)findViewById(R.id.deleteConcertButton);
btn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
db.deleteConcert(num);
}
});
} while (c.moveToNext());
}
}
catch (SQLException e)
{
e.printStackTrace();
}
db.close();
}
}
这是我的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
{
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)
{
/*
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(long ROW_ID, String name, String venue, String date, String comments)
{
ContentValues args = new ContentValues();
args.put(COL_NAME, name);
args.put(COL_VENUE, venue);
args.put(COL_DATE, date);
args.put(COL_COMMENTS, comments);
return db.update(DB_TABLE, args, COL_ROWID + "=" + ROW_ID, null) > 0;
}
}
答案 0 :(得分:1)
您需要在片段或活动中向onResume添加代码,以使列表更新光标,因此还需要更新列表视图以删除已删除的值
似乎您可能正在向现有列表添加新列表。在OnResume中你需要做的是更新data_list并调用concertList.notifyDataSetChanged();.对于来自DB的列表,我使用CursorAdapter而不是数组适配器,因为实际的机制不是100%熟悉
答案 1 :(得分:0)
两件事,关于db id与列表位置的评论是正确的。您将需要查询数据库不是基于列表中的位置,而是基于列表中的值。 (即,如果它是唯一的),以便从数据库中获取详细视图/记录。
第二个/第一个是当你选择列表并进入详细视图时列表活动没有消失/没有被销毁,所以当你回到列表时它仍然会显示之前查询过的值。
您需要在片段或活动中向onResume添加代码,以使列表更新光标,因此还需要更新列表视图以删除已删除的值