我有图像所示的视图我获取数据并显示它但它只显示两个记录
数据库有33个项目需要显示全部,但它只显示两个
需要一些帮助
提前致谢
请再次帮助快速致谢
谢谢你
我有图像所示的视图我获取数据并显示它但它只显示两个记录
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your activity_main.xml
getLayoutInflater().inflate(R.layout.activity_main, contentFrameLayout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().getItem(0).setChecked(true);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
DbHelper dbhelper = new DbHelper(this);
try {
dbhelper.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
districtList = dbhelper.getAllDistricts();
adapter = new DistrictsAdapter(this, districtList);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
// DbHelper Method
public List<Districts> getAllDistricts() {
List<Districts> districtList = new ArrayList<>();
String sql = "SELECT * FROM district;";
Cursor cursor = getReadableDatabase().rawQuery(sql, null);
try {
if (cursor.moveToFirst()) {
do {
Districts district = new Districts();
district.setId(Integer.parseInt(cursor.getString(0)));
district.setName(cursor.getString(1));
district.setThumbnail(cursor.getBlob(2));
district.setFortscount(Integer.parseInt(cursor.getString(3)));
districtList.add(district);
}
while (cursor.moveToNext());
}
}
catch(Exception e){e.printStackTrace();}
finally {
cursor.close();
}
return districtList;
}
// DISTRICTSADAPTER
public class DistrictsAdapter extends RecyclerView.Adapter<DistrictsAdapter.MyViewHolder> {
private Context mContext;
private List<Districts> districtList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, forts;
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
forts = (TextView) view.findViewById(R.id.forts);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
public DistrictsAdapter(Context mContext, List<Districts> districtList) {
this.mContext = mContext;
this.districtList = districtList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.district_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Districts district = districtList.get(position);
holder.name.setText(district.getName());
holder.forts.setText(district.getFortscount() + " किल्ले");
// loading album cover using Glide library
Glide.with(mContext).load(district.getThumbnail()).into(holder.thumbnail);
}
@Override
public int getItemCount() {
return districtList.size();
}
}
答案 0 :(得分:0)
尝试使用此代码,并根据您的代码更改表名和字段。
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;
}