我是Java,Android和SQLite的新手,所以我很惊讶我已经走到了这一步。
我正在尝试为我的盆景收藏创建一个简单的编目应用程序,该应用程序在MainActivity上的RecyclerView中显示我的收藏摘要。
我有数据库工作,因为我可以添加项目(通过DB Browser for SQLite检查)。我遇到的麻烦是在RecyclerView中显示这些数据。目前它只是显示laucher图标占位符图像,而不是使用来自DB的数据填充TextView。 RecyclerView中显示的视图数与数据库中的条目数相对应。
我不确定我做错了什么,所以非常感谢任何帮助。
MainActivity.java
Datasource mDataSource;
Button btnAddNew;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataSource = new Datasource(this);
mDataSource.open();
List<DataModel> listFromDB = mDataSource.getAllItems();
DataItemAdapter adapter = new DataItemAdapter(this, listFromDB);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvItems);
recyclerView.setAdapter(adapter);
btnAddNew = (Button) findViewById(R.id.btnInsert);
}
DataModel.Java
public class DataModel {
private String itemId;
private String itemName;
private String itemCommonName;
public DataModel() {
}
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemCommonName() {
return itemCommonName;
}
public void setItemCommonName(String itemCommonName) {
this.itemCommonName = itemCommonName;
}
Datasource.java
public class Datasource {
private Context mContext;
private SQLiteDatabase mDatabase;
SQLiteOpenHelper mDbHelper;
public Datasource(Context context){
this.mContext = context;
mDbHelper = new DatabaseHelper(mContext);
mDatabase = mDbHelper.getWritableDatabase();
}
public void open(){
mDatabase = mDbHelper.getWritableDatabase();
}
public void close(){
mDatabase.close();
}
public boolean insertData(String name, String commonName, String scientificName){
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);
contentValues.put(COL_COMMON_NAME, commonName);
contentValues.put(COL_SCI_NAME, scientificName);
long result = db.insert(DATABASE_TABLE, null, contentValues);
db.close();
if(result == -1) {
return false;
}else{
return true;
}
}
public List<DataModel> getAllItems(){
List<DataModel> dataModels = new ArrayList<>();
Cursor cursor = mDatabase.query(DATABASE_TABLE, ItemsTable.ALL_COLUMNS,
null, null, null, null, null);
while (cursor.moveToNext()){
DataModel item = new DataModel();
item.setItemId(cursor.getString(
cursor.getColumnIndex(ItemsTable.COL_ID)));
item.setItemId(cursor.getString(
cursor.getColumnIndex(ItemsTable.COL_NAME)));
item.setItemId(cursor.getString(
cursor.getColumnIndex(ItemsTable.COL_COMMON_NAME)));
item.setItemId(cursor.getString(
cursor.getColumnIndex(ItemsTable.COL_SCI_NAME)));
dataModels.add(item);
}
return dataModels;
}
DataItemAdapter.java
public class DataItemAdapter extends RecyclerView.Adapter<DataItemAdapter.ViewHolder> {
private List<DataModel> mItems;
private Context mContext;
public DataItemAdapter(Context context, List<DataModel> items) {
this.mContext = context;
this.mItems = items;
}
@Override
public DataItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View itemView = inflater.inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(DataItemAdapter.ViewHolder holder, int position) {
final DataModel item = mItems.get(position);
holder.tvName.setText(item.getItemName());
holder.tvCommonName.setText(item.getItemCommonName());
}
@Override
public int getItemCount() {
return mItems.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvName, tvCommonName;
public ImageView ivProfilePic;
public View mView;
public ViewHolder(View itemView){
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.tvName);
tvCommonName = (TextView) itemView.findViewById(R.id.tvCommonName);
ivProfilePic = (ImageView) itemView.findViewById(R.id.ivProfilePic);
mView = itemView;
}
}
activity_main.xml中的RecyclerView对象
<android.support.v7.widget.RecyclerView
android:id="@+id/rvItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"/>
TextViews和图像视图list_item.xml
<ImageView
android:id="@+id/ivProfilePic"
android:layout_width="75dp"
android:layout_height="75dp"
app:srcCompat="@mipmap/ic_launcher_round"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:contentDescription="Profile Pic"
tools:ignore="HardcodedText"/>
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="19dp"
android:layout_toEndOf="@+id/ivProfilePic"
android:text="TextView"
android:textSize="18sp"
android:visibility="visible"/>
<TextView
android:id="@+id/tvCommonName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:visibility="visible"/>
MainActivity目前的截图:
需要提供任何其他信息,只需询问,我会尝试发布。
答案 0 :(得分:0)
对它进行排序,现在看起来很简单。
在Cursor中,在复制并粘贴第一行(仅列名称)之后,我没有将项目设置器更改为正确的项目设置器。
此外,我将ID列标识为字符串,它应该是一个整数 - 也在DataModel类中更新了
while (cursor.moveToNext()){
DataModel item = new DataModel();
item.setItemId(cursor.getInt(
cursor.getColumnIndex(ItemsTable.COL_ID)));
item.setItemName(cursor.getString(
cursor.getColumnIndex(ItemsTable.COL_NAME)));
item.setItemCommonName(cursor.getString(
cursor.getColumnIndex(ItemsTable.COL_COMMON_NAME)));
item.setItemSciName(cursor.getString(
cursor.getColumnIndex(ItemsTable.COL_SCI_NAME)));
dataModels.add(item);
}
return dataModels;
}