我创建了一个跟踪课程信息的应用程序。目前,唯一可以动态更改的字段是modcode
,目前,其余所有字段均已硬编码为输入数字1。
这是插入代码:
public boolean insertData(String ModCode,String ModDesc,int UniID,int FacID,int EduID,int NoOfTest,int NoOfAss,int NoOfPrac,int NoOfExam)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col_2,ModCode);
contentValues.put(Col_3,ModDesc);
contentValues.put(Col_4,UniID);
contentValues.put(Col_5,FacID);
contentValues.put(Col_6,EduID);
contentValues.put(Col_7,NoOfTest);
contentValues.put(Col_8,NoOfAss);
contentValues.put(Col_9,NoOfPrac);
contentValues.put(Col_10,NoOfExam);
long result = db.insert(Table_Name, null, contentValues);
if(result==-1)
return false;
else
return true;
}
和插入的前端侧:
db = new DatabaseHelper(this);
textModCode = (EditText)findViewById(R.id.textModCode);
button_add = (Button)findViewById(R.id.button_add);
AddData();
按钮按下:
public void AddData(){
button_add.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v){
boolean isInserted = db.insertData(textModCode.getText().toString(),"1",1,1,1,1,1,1,1);
if(isInserted=true)
Toast.makeText(MainActivity.this,"Data in",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Sorry",Toast.LENGTH_LONG).show();
refresh();
}
}
);
}
刷新:
public void refresh()
{
Cursor res = db.getAllData();
moduleList = new ArrayList<>();
if (res != null ) {
if (res.moveToFirst()) {
do {
Module m = new Module(res.getInt(res.getColumnIndex("ModID")),res.getInt(res.getColumnIndex("NoOfTest")),res.getInt(res.getColumnIndex("NoOfAss")),res.getInt(res.getColumnIndex("NoOfPrac")),res.getInt(res.getColumnIndex("NoOfExam")),res.getString(res.getColumnIndex("ModCode")),res.getString(res.getColumnIndex("ModDesc")),res.getString(res.getColumnIndex("UniID")),res.getString(res.getColumnIndex("FacID")),res.getString(res.getColumnIndex("EduID")));
moduleList.add(m);
}while (res.moveToNext());
}
}
/*
*/
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
moduleAdapter = new ModuleAdapter(this, moduleList);
recyclerView.setAdapter(moduleAdapter);
}
“回收者”视图最初显示的是正确的所有内容,但是在滚动之后,随着每次向上或向下滚动,数字都会开始变化。
我做错了什么还是怎么回事?谢谢您或任何帮助
ModuleAdapter类代码
public class ModuleAdapter extends RecyclerView.Adapter<ModuleAdapter.ModuleViewHolder>{
private Context mCtx;
private List<Module> moduleList;
public ModuleAdapter(Context mCtx, List<Module> moduleList) {
this.mCtx = mCtx;
this.moduleList = moduleList;
}
@NonNull
@Override
public ModuleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.from(parent.getContext())
.inflate(R.layout.module_list, parent, false);
return new ModuleViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ModuleViewHolder holder, int position) {
Module module = moduleList.get(position);
holder.textModCodeView.setText(module.getModcode());
holder.textModuleDescView.setText(module.getModdesc());
holder.textEducatorView.setText(holder.textEducatorView.getText()+ module.getEducator());
holder.textNoOfTestView.setText(holder.textNoOfTestView.getText()+ String.valueOf(module.getNooftest()));
holder.textNoOfAssView.setText(holder.textNoOfAssView.getText()+ String.valueOf(module.getNoofass()));
holder.textNoOfPracView.setText(holder.textNoOfPracView.getText()+ String.valueOf(module.getNoofprac()));
holder.textNoOfExamView.setText(holder.textNoOfExamView.getText()+ String.valueOf(module.getNoofexam()));
Random r = new Random();
int red=r.nextInt(255 - 0 + 1)+0;
int green=r.nextInt(255 - 0 + 1)+0;
int blue=r.nextInt(255 - 0 + 1)+0;
GradientDrawable draw = new GradientDrawable();
draw.setShape(GradientDrawable.RECTANGLE);
draw.setColor(Color.rgb(red,green,blue));
holder.textModCodeView.setBackground(draw);
}
@Override
public int getItemCount() {
return moduleList.size();
}
class ModuleViewHolder extends RecyclerView.ViewHolder{
TextView textModCodeView,textModuleDescView,textEducatorView,textNoOfTestView,textNoOfAssView,textNoOfPracView,textNoOfExamView;
public ModuleViewHolder(View itemView) {
super(itemView);
textModCodeView = itemView.findViewById(R.id.textModCodeView);
textModuleDescView = itemView.findViewById(R.id.textModuleDescView);
textEducatorView = itemView.findViewById(R.id.textEducatorView);
textNoOfTestView = itemView.findViewById(R.id.textNoOfTestView);
textNoOfAssView = itemView.findViewById(R.id.textNoOfAssView);
textNoOfPracView = itemView.findViewById(R.id.textNoOfPracView);
textNoOfExamView = itemView.findViewById(R.id.textNoOfExamView);
}
}
}
答案 0 :(得分:1)
按照此步骤
1。在添加所有值moduleList.clear();
刷新:
public void refresh()
{
moduleList.clear(); // add this line
Cursor res = db.getAllData();
moduleList = new ArrayList<>();
if (res != null ) {
if (res.moveToFirst()) {
do {
Module m = new Module(res.getInt(res.getColumnIndex("ModID")),res.getInt(res.getColumnIndex("NoOfTest")),res.getInt(res.getColumnIndex("NoOfAss")),res.getInt(res.getColumnIndex("NoOfPrac")),res.getInt(res.getColumnIndex("NoOfExam")),res.getString(res.getColumnIndex("ModCode")),res.getString(res.getColumnIndex("ModDesc")),res.getString(res.getColumnIndex("UniID")),res.getString(res.getColumnIndex("FacID")),res.getString(res.getColumnIndex("EduID")));
moduleList.add(m);
}while (res.moveToNext());
}
}
/*
*/
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
moduleAdapter = new ModuleAdapter(this, moduleList);
recyclerView.setAdapter(moduleAdapter);
}
2。更改下面的代码,如我在下面提到的(我认为视图被限制)
holder.textEducatorView.setText("Educatore"+ module.getEducator());
holder.textNoOfTestView.setText("NoOfTestView"+ String.valueOf(module.getNooftest()));
holder.textNoOfAssView.setText("NoOfAssView"+ String.valueOf(module.getNoofass()));
holder.textNoOfPracView.setText("NoOfPracView"+ String.valueOf(module.getNoofprac()));
holder.textNoOfExamView.setText("textNoOfExamView"+ String.valueOf(module.getNoofexam()));