我正在使用“回收者视图”方法创建列表。但是,如果我插入数据。RecyclerView显示所有行,但最后插入的数据显示在所有先前行上。我在Sqlite数据库的所有行数据中检查了SQLite数据库中的数据是否正确/正确,但在RecyclerView中却发现了问题。
i用调试模式检查了所有数据。它们从SQLite数据库到Cursor都正确了;我将Coursor分配给List;在OtherClass中,数据从List中得到了正确的。但是从列表到Recyclerview不正确。
查看此输出 Please See The OUTPUT OfScreen on The Image Click on this Link
模型类:
公共类服务{
public int VehicleID;
public String VehicleMake;
public String VehicleModel;
public String VehicleReg;
public String CustomerName;
public String Contact;
public String ServiceType;
public String Amount;
public String Party;
public String Comments;
public int getVehicleID() {
return VehicleID;
}
public void setVehicleID(int vehicleID) {
VehicleID = vehicleID;
}
public String getVehicleMake() {
return VehicleMake;
}
public void setVehicleMake(String vehicleMake) {
VehicleMake = vehicleMake;
}
public String getVehicleModel() {
return VehicleModel;
}
public void setVehicleModel(String vehicleModel) {
VehicleModel = vehicleModel;
}
public String getVehicleReg() {
return VehicleReg;
}
public void setVehicleReg(String vehicleReg) {
VehicleReg = vehicleReg;
}
public String getCustomerName() {
return CustomerName;
}
public void setCustomerName(String customerName) {
CustomerName = customerName;
}
public String getContact() {
return Contact;
}
public void setContact(String contact) {
Contact = contact;
}
public String getServiceType() {
return ServiceType;
}
public void setServiceType(String serviceType) {
ServiceType = serviceType;
}
public String getAmount() {
return Amount;
}
public void setAmount(String amount) {
Amount = amount;
}
public String getParty() {
return Party;
}
public void setParty(String party) {
Party = party;
}
public String getComments() {
return Comments;
}
public void setComments(String comments) {
Comments = comments;
}
}
MngServices类: //从数据库中获取数据
public class MngServices extends SQLiteOpenHelper
{
SQLiteDatabase db;
Services mServies = new Services(); //ModelClass
ArrayList<Services> lstmService; //List
//Database Variables
public static final String DB_NAME = "CarWashDB.db"; //database file
public static final int DB_VERSION = 1;
public static final String TABLE_NAME = "tblService"; ..tableName
public static final String VehicleID = "VehicleID";
public static final String VehicleMake = "VehicleMake";
public static final String VehicleModel = "VehicleModel";
public static final String VehicleReg = "VehicleReg";
public static final String CustomerName = "CustomerName";
public static final String Contact = "Contact";
public static final String ServiceType = "ServiceType";
public static final String Amount = "Amount";
public static final String Party = "Party";
public static final String Comments = "Comments";
//TODO Constructor
public MngServices(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " ( " + VehicleID + " INTEGER PRIMARY KEY AUTOINCREMENT," + VehicleMake + " TEXT, " + VehicleModel + " TEXT, " + VehicleReg + " TEXT, " + CustomerName + " TEXT, " + Contact + " TEXT, " + ServiceType + " TEXT, " + Amount + " TEXT, " + Party + " TEXT, " + Comments + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertService(Services mS){
db = this.getWritableDatabase();
db.execSQL("INSERT INTO " + TABLE_NAME + "(" + VehicleMake + "," + VehicleModel + "," + VehicleReg + "," + CustomerName + "," + Contact + "," + ServiceType + "," + Amount + "," + Party + "," + Comments + ") VALUES('" + mS.getVehicleMake() + "','" + mS.getVehicleModel() + "','" + mS.getVehicleReg() + "','" + mS.getCustomerName() + "','" + mS.getContact() + "','" + mS.getServiceType() + "','" + mS.getAmount() + "','" + mS.getParty() + "','" + mS.getComments() + "')");
db.close();
if(db.toString() == "-1")
return false;
else
return true;
}
public ArrayList <Services> getAllService(){
lstmService = new ArrayList<Services>();
db = this.getReadableDatabase();
// String selectQuery = String.format("Select VehicleMake,VehicleModel,VehicleReg,ServiceType,Amount,CustomerName from "+TABLE_NAME,null);
Cursor c = db.rawQuery("Select "+VehicleMake+","+VehicleModel+","+VehicleReg+","+ServiceType+","+Amount+","+CustomerName+ " from " +TABLE_NAME,null);
if(c.getCount() > 0){
// for (int i = 0; i < c.getCount(); i++) {
while( c.moveToNext()){
// mServies.setVehicleID(Integer.parseInt(c.getString((c.getColumnIndexOrThrow( "VehicleID")))));
mServies.setVehicleMake(c.getString(c.getColumnIndexOrThrow("VehicleMake")));
mServies.setVehicleModel(c.getString(c.getColumnIndexOrThrow("VehicleModel")));
mServies.setVehicleReg(c.getString(c.getColumnIndexOrThrow("VehicleReg")));
mServies.setServiceType(c.getString(c.getColumnIndex("ServiceType")));
mServies.setAmount(c.getString(c.getColumnIndex("Amount")));
mServies.setCustomerName(c.getString(5));
// Log.d("ListError ", String.valueOf( a +": "+ mServies.VehicleMake +" = "+ mServies.VehicleModel+" = "+mServies.VehicleReg+" = "+mServies.ServiceType+" = "+ mServies.Amount+" = "+mServies.CustomerName));
lstmService.add(mServies);
}
}
c.close();
db.close();
return lstmService;
}
}
ADPATER CLASS //用于RecyclreView绑定数据
//OuterClass . InnerClass
public class ServiceRAdapter extends
RecyclerView.Adapter<ServiceRAdapter.ServiceAdapterInnerClass>
{
Context context;
ArrayList<Services> ArrLstService;
//Outer Class Constructor
public ServiceRAdapter(Context context, ArrayList<Services> data) {
this.context = context;
this.ArrLstService = data;
}
@NonNull
@Override
public ServiceAdapterInnerClass onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(context).inflate(R.layout.service_list_row, viewGroup ,false);
return new ServiceAdapterInnerClass(v);
}
@Override
public void onBindViewHolder(@NonNull ServiceAdapterInnerClass serviceAdapterInnerClass, int i) {
//Inner Class Views & ModelList-Attr binding
serviceAdapterInnerClass.VehicleMake.setText(ArrLstService.get(i).getVehicleMake());
serviceAdapterInnerClass.VehicleModel.setText(ArrLstService.get(i).getVehicleModel());
serviceAdapterInnerClass.VehicleReg.setText(ArrLstService.get(i).getVehicleReg());
serviceAdapterInnerClass.CustomerName.setText(ArrLstService.get(i).getCustomerName());
serviceAdapterInnerClass.ServiceType.setText(ArrLstService.get(i).getServiceType());
serviceAdapterInnerClass.Amount.setText(ArrLstService.get(i).getAmount());
Log.d("getVehicleMake:", String.valueOf(ArrLstService.get(i).getVehicleMake()));
Log.d("getVehicleModel:", String.valueOf(ArrLstService.get(i).getVehicleModel()));
Log.d("getVehicleReg:", String.valueOf(ArrLstService.get(i).getVehicleReg()));
Log.d("getCustomerName:", String.valueOf(ArrLstService.get(i).getCustomerName()));
Log.d("getServiceType:", String.valueOf(ArrLstService.get(i).getServiceType()));
Log.d("getAmount:", String.valueOf(ArrLstService.get(i).getAmount()));
}
@Override
public int getItemCount() {
return ArrLstService.size();
}
// -> Inner Class <-
class ServiceAdapterInnerClass extends RecyclerView.ViewHolder {
TextView VehicleMake,ServiceType,VehicleModel,VehicleReg,CustomerName,Amount;
//Inner Class Constructor
public ServiceAdapterInnerClass(@NonNull View itemView) {
super(itemView);
VehicleMake = (TextView)itemView.findViewById(R.id.trVehicleMake);
ServiceType= (TextView)itemView.findViewById(R.id.trServiceType);
VehicleModel = (TextView)itemView.findViewById(R.id.trVehicleModel);
VehicleReg = (TextView)itemView.findViewById(R.id.trVehicleReg);
CustomerName = (TextView)itemView.findViewById(R.id.trCustomerName);
Amount = (TextView)itemView.findViewById(R.id.trAmount);
}
}//Inner Class
}//Outer Class
主类
public class ServicesList extends AppCompatActivity
{
RecyclerView recyclerID;
ServiceRAdapter adapter;
ArrayList<Services> sList;
ListView lvListID; //Header-List_ID
ViewGroup headerview; //For Header
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services_list);//Main List Layout
headerRows();
tableRows();
}
private void headerRows() {
lvListID =(ListView) findViewById(R.id.lvListID);
headerview = (ViewGroup)getLayoutInflater() .inflate(R.layout.header_services,lvListID,false);
lvListID.addHeaderView(headerview); //Adding Header
}
private void tableRows() {
MngServices objServices = new MngServices(this);
recyclerID =(RecyclerView) findViewById(R.id.recyclerID);
recyclerID.setHasFixedSize(true);
recyclerID.setLayoutManager(new LinearLayoutManager(this));
sList =objServices.getAllService();
//---------------------------------------------//
adapter = new ServiceRAdapter( ServicesList.this,sList);
recyclerID.setAdapter(adapter);
}
}
答案 0 :(得分:1)
在您的getAllService()
方法中,您将为每个循环迭代重用相同的Services
对象。为每一行实例化一个新对象,例如
while( c.moveToNext()){
mServies = new Services();
mServies.set...();
lstmService.add(mServies);
}
答案 1 :(得分:0)
尝试如下插入数据 例如
public boolean insertData(String name,String surname,String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
在数据库中的getAllService()下,首先将光标移至第一位置
public ArrayList <Services> getAllService(){
lstmService = new ArrayList<Services>();
db = this.getReadableDatabase();
// String selectQuery = String.format("Select VehicleMake,VehicleModel,VehicleReg,ServiceType,Amount,CustomerName from "+TABLE_NAME,null);
Cursor c = db.rawQuery("select * from " +TABLE_NAME,null);
if(c != null && c.getCount() > 0){
c.moveToFrist()
do{
// mServies.setVehicleID(Integer.parseInt(c.getString((c.getColumnIndexOrThrow( "VehicleID")))));
mServies.setVehicleMake(c.getString(c.getColumnIndexOrThrow("VehicleMake")));
mServies.setVehicleModel(c.getString(c.getColumnIndexOrThrow("VehicleModel")));
mServies.setVehicleReg(c.getString(c.getColumnIndexOrThrow("VehicleReg")));
mServies.setServiceType(c.getString(c.getColumnIndex("ServiceType")));
mServies.setAmount(c.getString(c.getColumnIndex("Amount")));
mServies.setCustomerName(c.getString(5));
// Log.d("ListError ", String.valueOf( a +": "+ mServies.VehicleMake +" = "+ mServies.VehicleModel+" = "+mServies.VehicleReg+" = "+mServies.ServiceType+" = "+ mServies.Amount+" = "+mServies.CustomerName));
lstmService.add(mServies);
}while(c.moveToNext())
}
c.close();
db.close();
return lstmService;
}