使用 SimpleCursor Loader 在 ListView 中显示元素
DIsplaying element in ListView using SimpleCursor Loader
我有以下片段,它出现在一个选项卡中。
public class Tab3Fragment extends Fragment {
SQLiteDatabase db;
DBHandler dbHandler = new DBHandler(getContext(), null, null, 4);
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab3_frag, container, false);
db = dbHandler.getReadableDatabase();
String [] columns = {Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME};
Cursor mCursor = db.query(Contract.Student_Table.TABLE_NAME, columns, null,
null, null, null, null);
String[] from = new String[]{Contract.Student_Table.STUDENTS_COLUMN_NAME};
int[] to = new int[]{R.id.student_view};
SimpleCursorAdapter mCurAdapter = new SimpleCursorAdapter(getContext(),android.R.layout.simple_list_item_1, mCursor, from, to,0);
ListView mMyListView = view.findViewById(R.id.simple_list);
mMyListView.setAdapter(mCurAdapter);
return view;
}
}
我有以下 XML 文件 (tab3_frag)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c2f6ff"
android:orientation="vertical">
<ListView
android:id="@+id/simple_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/student_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
但是我的代码似乎不起作用,而且我无法发现错误。没有崩溃或错误,我的选项卡中什么也没有出现。如果能指导我解决我可能做错的问题,我将不胜感激。
改变这件事
db = dbHandler.getReadableDatabase();
更改以下内容
像
一样为文本视图制作单独的布局
list_template.xml
<TextView
android:id="@+id/student_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
然后改变这个东西..
int[] to = {R.id.student_view};
SimpleCursorAdapter mCurAdapter = new SimpleCursorAdapter(getContext(),R.layout.list_template, mCursor, from, to,0);
下面class是一个单独的class用于执行所有操作。
public class DBHelper extends SQLiteOpenHelper{
//USER TABLE
public static final String USER_TABLE_NAME = "MyTableTwo";
public static final String USER_COLUMN_USER_ID = "id";
public static final String USER_COLUMN_USER_NAME = "Item_Name";
public static final String USER_COLUMN_USER_ADDRESS = "Weight";
public static final String USER_COLUMN_USER_CREATED_AT = "MRP";
public static final String USER_COLUMN_USER_UPDATED_AT = "BARCODE";
public DBHelper(Context context,
String dbName,
Integer version) {
super(context, dbName, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
tableCreateStatements(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
onCreate(db);
}
private void tableCreateStatements(SQLiteDatabase db) {
try {
db.execSQL(
"CREATE TABLE IF NOT EXISTS "
+ USER_TABLE_NAME + "("
+ USER_COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ USER_COLUMN_USER_NAME + " VARCHAR(20), "
+ USER_COLUMN_USER_ADDRESS + " VARCHAR(50), "
+ USER_COLUMN_USER_CREATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ", "
+ USER_COLUMN_USER_UPDATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ")"
);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<MyTableTwo> getMyItems() {
List<MyTableTwo> mySuperList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + USER_TABLE_NAME ;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
MyTableTwo myTable = new MyTableTwo();
myTable.itemName = (c.getString(c.getColumnIndex("Item_Name")));
myTable.weight = (c.getString(c.getColumnIndex("Weight")));
myTable.mrp = (c.getString(c.getColumnIndex("MRP")));
myTable.barcod = (c.getString(c.getColumnIndex("BARCODE")));
mySuperList.add(myTable);
c.moveToNext();
}
}
return mySuperList;
}
public Long insertUser(MyTableTwo user) throws Exception {
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(USER_COLUMN_USER_NAME, user.itemName);
contentValues.put(USER_COLUMN_USER_ADDRESS, user.barcod);
contentValues.put(USER_COLUMN_USER_CREATED_AT, user.weight);
contentValues.put(USER_COLUMN_USER_UPDATED_AT, user.barcod);
return db.insert(USER_TABLE_NAME, null, contentValues);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private String getCurrentTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
}
我有以下片段,它出现在一个选项卡中。
public class Tab3Fragment extends Fragment {
SQLiteDatabase db;
DBHandler dbHandler = new DBHandler(getContext(), null, null, 4);
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab3_frag, container, false);
db = dbHandler.getReadableDatabase();
String [] columns = {Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME};
Cursor mCursor = db.query(Contract.Student_Table.TABLE_NAME, columns, null,
null, null, null, null);
String[] from = new String[]{Contract.Student_Table.STUDENTS_COLUMN_NAME};
int[] to = new int[]{R.id.student_view};
SimpleCursorAdapter mCurAdapter = new SimpleCursorAdapter(getContext(),android.R.layout.simple_list_item_1, mCursor, from, to,0);
ListView mMyListView = view.findViewById(R.id.simple_list);
mMyListView.setAdapter(mCurAdapter);
return view;
}
}
我有以下 XML 文件 (tab3_frag)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c2f6ff"
android:orientation="vertical">
<ListView
android:id="@+id/simple_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/student_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
但是我的代码似乎不起作用,而且我无法发现错误。没有崩溃或错误,我的选项卡中什么也没有出现。如果能指导我解决我可能做错的问题,我将不胜感激。
改变这件事
db = dbHandler.getReadableDatabase();
更改以下内容
像
一样为文本视图制作单独的布局list_template.xml
<TextView
android:id="@+id/student_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
然后改变这个东西..
int[] to = {R.id.student_view};
SimpleCursorAdapter mCurAdapter = new SimpleCursorAdapter(getContext(),R.layout.list_template, mCursor, from, to,0);
下面class是一个单独的class用于执行所有操作。
public class DBHelper extends SQLiteOpenHelper{
//USER TABLE
public static final String USER_TABLE_NAME = "MyTableTwo";
public static final String USER_COLUMN_USER_ID = "id";
public static final String USER_COLUMN_USER_NAME = "Item_Name";
public static final String USER_COLUMN_USER_ADDRESS = "Weight";
public static final String USER_COLUMN_USER_CREATED_AT = "MRP";
public static final String USER_COLUMN_USER_UPDATED_AT = "BARCODE";
public DBHelper(Context context,
String dbName,
Integer version) {
super(context, dbName, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
tableCreateStatements(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
onCreate(db);
}
private void tableCreateStatements(SQLiteDatabase db) {
try {
db.execSQL(
"CREATE TABLE IF NOT EXISTS "
+ USER_TABLE_NAME + "("
+ USER_COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ USER_COLUMN_USER_NAME + " VARCHAR(20), "
+ USER_COLUMN_USER_ADDRESS + " VARCHAR(50), "
+ USER_COLUMN_USER_CREATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ", "
+ USER_COLUMN_USER_UPDATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ")"
);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<MyTableTwo> getMyItems() {
List<MyTableTwo> mySuperList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + USER_TABLE_NAME ;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
MyTableTwo myTable = new MyTableTwo();
myTable.itemName = (c.getString(c.getColumnIndex("Item_Name")));
myTable.weight = (c.getString(c.getColumnIndex("Weight")));
myTable.mrp = (c.getString(c.getColumnIndex("MRP")));
myTable.barcod = (c.getString(c.getColumnIndex("BARCODE")));
mySuperList.add(myTable);
c.moveToNext();
}
}
return mySuperList;
}
public Long insertUser(MyTableTwo user) throws Exception {
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(USER_COLUMN_USER_NAME, user.itemName);
contentValues.put(USER_COLUMN_USER_ADDRESS, user.barcod);
contentValues.put(USER_COLUMN_USER_CREATED_AT, user.weight);
contentValues.put(USER_COLUMN_USER_UPDATED_AT, user.barcod);
return db.insert(USER_TABLE_NAME, null, contentValues);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private String getCurrentTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
}