我使用Android Library SQLiteAssetHelper来读取Sqlite数据库文件,并从SQLite数据库中填充我的RecycleryView ...
这是我的DataBase Helper:
class Spam:
_instances = WeakSet()
def __new__(cls, *args, **kwargs):
# Create the instance
instance = super().__new__(cls, *args, **kwargs)
# Keep track of the instance
cls._instances.add(instance)
# Return the instance as __new__ would
return instance
这是我的POJO类Office.java:
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db.sql";
private static final int DATABASE_VERSION = 1;
private static final String ID="ID";
private static final String NAME="Name";
private static final String IMAGE="Img";
private static final String Location="Location";
private static final String Offices_Table="Offices";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public ArrayList<Office> GetOffices(){
SQLiteDatabase sqLiteDatabase=getWritableDatabase();
String[] columns={DBHelper.ID,DBHelper.NAME,DBHelper.IMAGE,DBHelper.Location};
Cursor cursor=sqLiteDatabase.query(DBHelper.Offices_Table, columns, null, null, null, null, null);
ArrayList<Office> offices = new ArrayList<>();
while(cursor.moveToNext()){
Office office=new Office();
office.officeID=cursor.getInt(cursor.getColumnIndex(DBHelper.ID));
office.officeName=cursor.getString(cursor.getColumnIndex(DBHelper.NAME));
office.officeLocation=cursor.getString(cursor.getColumnIndex(DBHelper.Location));
offices.add(office);
}
return offices;
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}
初始化DatabaseHelper文件并将数据从sqlite数据库文件加载到OfficesActivity.java文件中的Application
public class Office {
public int officeID;
public String officeName;
public int officeImg;
public String officeInfo;
public int officeRate;
public String officeLocation;
public Office(String officeName, int officeImg , String officeLocation) {
this.officeName = officeName;
this.officeImg = officeImg;
this.officeLocation=officeLocation;
}
public Office() {
}
public Office(int officeID, String officeName, int officeImg, String officeInfo, int officeRate, String officeLocation) {
this.officeID = officeID;
this.officeName = officeName;
this.officeImg = officeImg;
this.officeInfo = officeInfo;
this.officeRate = officeRate;
this.officeLocation = officeLocation;
}
public void setOfficeID(int officeID) {
this.officeID = officeID;
}
public void setOfficeName(String officeName) {
this.officeName = officeName;
}
public void setOfficeImg(int officeImg) {
this.officeImg = officeImg;
}
public void setOfficeInfo(String officeInfo) {
this.officeInfo = officeInfo;
}
public void setOfficeRate(int officeRate) {
this.officeRate = officeRate;
}
public void setOfficeLocation(String officeLocation) {
this.officeLocation = officeLocation;
}
public int getOfficeID() {
return officeID;
}
public String getOfficeName() {
return officeName;
}
public int getOfficeImg() {
return officeImg;
}
public String getOfficeInfo() {
return officeInfo;
}
public int getOfficeRate() {
return officeRate;
}
public String getOfficeLocation() {
return officeLocation;
}}
Tracetrack Log:
public class Offices extends AppCompatActivity {
RecyclerView recyclerView ;
OfficeAdapter officeAdapter;
List<Office> officeDataList;
List<Integer> officesImg;
DBHelper dbHelper;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offices);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview_paths);
officeDataList = dbHelper.GetOffices();
officeAdapter = new OfficeAdapter(this , officeDataList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(officeAdapter);
}}
我知道Office类属性为空,OfficeAdapter采用空列表,但是我怎么能解决这个问题呢?原因是什么?
问题解决了,感谢VSB ..
但我得到的错误是说那里没有这样的表......
日志错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artline.ministryoftourismpalestine/com.artline.ministryoftourismpalestine.Offices}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.artline.ministryoftourismpalestine.DBHelper.GetOffices()' on a null object reference
答案 0 :(得分:0)
您错过了初始化DBHelper。
public class Offices extends AppCompatActivity {
...
DBHelper dbHelper = new DBHelper (this);
....
}