我想要帮助从本地数据库SQLite在listview中显示数据。我已经在数据库中有数据了。 所以,请帮助我在哪里犯错误?
低于DatabseHelper.Java代码
private static final String DATABASE_NAME = "Barcode_Printing.db";
static final String TABLE_NAME = "Item_Master";
static final String Item_name = "Item_Name";
static final String wEight = "Weight";
static final String MrP = "MRP";
static final String BarcodE = "BARCODE";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + BarcodE + " INTEGER PRIMARY KEY, " + Item_name + " TEXT, " + wEight + " TEXT, " + MrP + " INTEGER, EXPIRY_DATE TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public Cursor getAllItems() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[]{BarcodE, Item_name, wEight, MrP},
null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
return cursor;
}
else
{
return null;
}
}
activity_dispaly_items.xml下面的
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.packlab.alpesh.barcodeprinting.Display_Items">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ListView
android:id="@+id/lvItem_Display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
tools:layout_editor_absoluteY="3dp" />
</android.support.design.widget.CoordinatorLayout>
items_display_layout.xml 下面的
以下DisplayItems.Java代码 它直接给我错误&#34;出错了!&#34;
它没有尝试。它直接进入Catch。<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.065"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.119"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvMrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.119"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvWeight" />
<TextView
android:id="@+id/tvWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.119"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvBarcode" />
</android.support.constraint.ConstraintLayout>
public class Display_Items extends AppCompatActivity
{
SimpleCursorAdapter simpleCursorAdapter;
ListView display_Items;
DatabaseHelper DatabaseHelper;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display__items);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
spinner = findViewById(R.id.spinner_Comapny);
setSupportActionBar(toolbar);
display_Items = findViewById(R.id.lvItem_Display);
DatabaseHelper = new DatabaseHelper(this);
loadSpinnerData();
displayProductList();
}
private void displayProductList()
{
try
{
Cursor cursor = DatabaseHelper.getAllItems();
if (cursor == null)
{
Toast.makeText(Display_Items.this, "Unable to generate cursor.", Toast.LENGTH_SHORT).show();
return;
}
if (cursor.getCount() == 0)
{
Toast.makeText(Display_Items.this, "No Products in the Database.", Toast.LENGTH_SHORT).show();
return;
}
String[] columns = new String[] {
DatabaseHelper.BarcodE,
DatabaseHelper.Item_name,
DatabaseHelper.wEight,
DatabaseHelper.MrP
};
int[] boundTo = new int[] {
R.id.tvBarcode,
R.id.tvItemName,
R.id.tvWeight,
R.id.tvMrp
};
simpleCursorAdapter = new SimpleCursorAdapter(this,
R.layout.item_display_layout,
cursor,
columns,
boundTo,
0);
display_Items.setAdapter(simpleCursorAdapter);
}
catch (Exception ex)
{
Toast.makeText(Display_Items.this, "There was an error!", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:1)
我提供了从代码下面使用的sqllite读取数据的简单方法..
首先制作一个pojo课程。
public class MyTable {
public String itemName,weight,mrp,barcod;
}
然后数据基类make方法读取数据...
public List<MyTable> getMyItems() {
List<MyTable> mySuperList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_NAME ;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
MyTable myTable = new MyTable();
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;
}
然后使用此方法并将数据绑定到listview。
答案 1 :(得分:1)
如上所述,您将数据绑定到listview然后制作自定义适配器并采用一种布局来显示不同视图控件中的所有数据。
adapter ::
public class CommentsAdapter extends ArrayAdapter<MyTable> {
Context context;
List<MyTable> data = null;
public CommentsAdapter(Context context, int layoutResourceId, List<MyTable> data) {
super(context, layoutResourceId, data);
this.context = context;
this.data = data;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
// if(context.get()!=null) {
final LayoutInflater inflater = ((Activity) context).getLayoutInflater();
final View outerContiner = inflater.inflate(R.layout.comment_item, parent, false);// define your layout
TextView text = (TextView) outerContiner.findViewById(R.id.comment_text);
TextView barcode = (TextView) outerContiner.findViewById(R.id.comment_barcode);
text.setText(data.get(position).itemName);
barcode.setText(data.get(position).barcode);
return outerContiner;
//}
}
}
然后使用下面的代码绑定数据.. 注意::这里布局在适配器构造函数和getView方法中相同。
List<MyTableTwo> myTableTwos=dbHelper.getMyItems();
CommentsAdapter commentsAdapter=new CommentsAdapter(DbInsert.this,R.layout.table_layout,myTableTwos);
ListView listView=findViewById(R.id.liLvData);
listView.setAdapter(commentsAdapter);
commentsAdapter.notifyDataSetChanged();