具有 ListView 可见性的 SimpleCursorAdapter
Posted
技术标签:
【中文标题】具有 ListView 可见性的 SimpleCursorAdapter【英文标题】:SimpleCursorAdapter with ListView visibility 【发布时间】:2019-05-29 17:27:33 【问题描述】:您好,我创建了一个数据库,我试图在列表视图中列出该数据库的名称,但没有显示任何内容。
mty 数据库的代码是:
public class LockedInDatabaseHelper extends SQLiteOpenHelper
private static final String DB_NAME = "Locked In";
private static final int DB_VERSION = 1;
public LockedInDatabaseHelper(Context context)
super(context, DB_NAME, null, DB_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE ARTISTS (_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "NAME TEXT,"
+ "INTRO TEXT,"
+"IMAGE_RESCOURCE_ID INT)");
insertArtist(db, "Jinx Inkz", "An artist who specializes in portraits and custom pieces", R.drawable.jinx_profile);
insertArtist(db, "Bazooka Zook", "An artist who specializes in clean line work and neo - traditional", R.drawable.zook_profile );
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
private static void insertArtist(SQLiteDatabase db, String name, String intro, int resourceId)
ContentValues artistInfo = new ContentValues();
artistInfo.put("NAME", name);
artistInfo.put("INTRO", intro);
artistInfo.put("IMAGE_RESOURCE_ID", resourceId);
db.insert("ARTISTS",null, artistInfo);
最后,我希望信息出现的地方的 java 代码是
public class ArtistsActivity extends AppCompatActivity
private SQLiteDatabase db;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artists);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ListView listArtists = (ListView) findViewById(R.id.artistsLV);
SQLiteOpenHelper lockedInDatabaseHelper = new LockedInDatabaseHelper(ArtistsActivity.this);
try
db = lockedInDatabaseHelper.getReadableDatabase();
cursor = db.query("ARTISTS",
new String[]"_id", "NAME",
null, null, null, null, null);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(ArtistsActivity.this,
android.R.layout.simple_list_item_1,
cursor,
new String[]"NAME",
new int[] android.R.id.text1,
0);
listArtists.setAdapter(listAdapter);
catch (SQLiteException e)
Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_LONG);
toast.show();
//Create a listener for clicks on listview
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> listArtists,
View itemView,
int position,
long id)
//Pass tha artist user clicks on to artistActivity
Intent intent = new Intent(ArtistsActivity.this, ArtistDetail.class);
intent.putExtra(ArtistDetail.EXTRA_ARTISTID, (int) id);
startActivity(intent);
;
//Assign the listener to list view
listArtists.setOnItemClickListener(itemClickListener);
@Override
public void onDestroy()
super.onDestroy();
cursor.close();
db.close();
我一直在研究代码,但我认为它与 ListView 无关。请帮忙!我超级卡住了,我只想要一个列表,我可以添加一个 onClickListener 以继续应用程序。
【问题讨论】:
【参考方案1】:您的问题是列名 IMAGE_RESOURCE_ID 拼写错误。也就是说,您使用列名 IMAGE_RESCOURCE_ID 创建表,但尝试插入列 IMAGE_RESOURCE_ID。
我建议始终对表名和列名使用常量,因此您只需定义一次,然后使用相同的常量来引用这些项目。
因此,我建议将数据库助手 LockedInDatabaseHelper.java 更改为 :-
public class LockedInDatabaseHelper extends SQLiteOpenHelper
private static final String DB_NAME = "Locked In";
private static final int DB_VERSION = 1;
public static final String TBL_ARTISTS = "ARTISTS";
public static final String COL_ID = BaseColumns._ID;
public static final String COl_NAME = "NAME";
public static final String COL_INTRO = "INTRO";
public static final String COL_IMAGERESOURCEID = "IMAGE_RESOURCE_ID";
public LockedInDatabaseHelper(Context context)
super(context, DB_NAME, null, DB_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE ARTISTS (" + COL_ID + " INTEGER PRIMARY KEY,"
+ COl_NAME + " TEXT,"
+ COL_INTRO + " TEXT,"
+ COL_IMAGERESOURCEID + " INT)");
insertArtist(db, "Jinx Inkz", "An artist who specializes in portraits and custom pieces", R.drawable.jinx_profile);
insertArtist(db, "Bazooka Zook", "An artist who specializes in clean line work and neo - traditional",R.drawable.zook_profile);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
private static void insertArtist(SQLiteDatabase db, String name, String intro, int resourceId)
ContentValues artistInfo = new ContentValues();
artistInfo.put(COl_NAME, name);
artistInfo.put(COL_INTRO, intro);
artistInfo.put(COL_IMAGERESOURCEID, resourceId);
db.insert(TBL_ARTISTS,null, artistInfo);
除了对 ArtistsActivity.java 进行了一些更改,还可以利用常量,以便它包含:-
cursor = db.query(LockedInDatabaseHelper.TBL_ARTISTS,
new String[]LockedInDatabaseHelper.COL_ID, "NAME",
null, null, null, null, null);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(ArtistsActivity.this,
android.R.layout.simple_list_item_1,
cursor,
new String[]LockedInDatabaseHelper.COl_NAME,
new int[] android.R.id.text1,
0);
我相信上述方法会奏效。 但是,由于您正在更改数据库的结构,因此您应该在重新运行应用程序之前执行以下操作之一:-
-
删除/清除应用程序的数据(这实际上会删除数据库)。
卸载应用程序(这也会有效地删除数据库)。
【讨论】:
以上是关于具有 ListView 可见性的 SimpleCursorAdapter的主要内容,如果未能解决你的问题,请参考以下文章
为啥 PHP 的空合并运算符 (??) 不能处理具有不同可见性的类常量?
特征可以具有具有私有和受保护可见性的属性和方法吗?特质可以有构造函数、析构函数和类常量吗?