我已经通过arraylist在我的数据库中插入数据,但它没有显示在数据库中。这是我的代码
public void fillData(){
// code to add the new Question
SQLiteDatabase db= this.getWritableDatabase();
//now i am making some questions model
Question_Activity_Model model =new Question_Activity_Model("whcih girl of our class can sing song?","xyz","Mehak tariq","abc","Alina",1,"A");
//this is one model object now i am saving it in our list first
DataCollection.get(context).fillQuestionsList(model);
model =new Question_Activity_Model("whcih girl of our class can sing song?","xyz","Mehak tariq","abc","Alina",1,"A");
DataCollection.get(context).fillQuestionsList(model);
//now two model objects are saved in arraylist..
///now i am getting data from arraylist
for (Question_Activity_Model questn: DataCollection.get(context).getQuestionsList()) {
//now i am getting all the models from the list and looping them one by one to get model object..i am using forech for looping..
ContentValues values= new ContentValues();
values.put(QuestId,questn.getId());
values.put(Quests,questn.getQuestion());
values.put(Option1,questn.getOption1());
values.put(Option2,questn.getOption2());
values.put(Option3,questn.getOption3());
values.put(Option4,questn.getOption4());
values.put(CorrectOption,questn.getCorrectoption());
db.insert("QuestionsTable", null, values);
db.close();
}
此代码位于扩展sqlite open helper class
的数据库类中- 这段代码在我的主要活动类
中public class Question_activity_main extends AppCompatActivity {
ListView listView;
Question_Activity_Model qm;
AppDatabase appDatabase;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question_activity_main);
listView = (ListView) findViewById(R.id.queslistView);
appDatabase= new AppDatabase(this);
ArrayList<Question_Activity_Model> question_activity_modelArrayList = new ArrayList<>();
Cursor c= appDatabase.retrieveQuests();
if (c.getCount()==0){
Toast.makeText(getApplicationContext(), "Nothing in db", Toast.LENGTH_SHORT).show();
}
else {
while (c.moveToNext()){
question_activity_modelArrayList.add(qm );
Question_Activity_Adapter question_activity_adapter= new Question_Activity_Adapter(this, question_activity_modelArrayList );
listView.setAdapter(question_activity_adapter);
}
}
这是我的Question_Activity_Adapter
的代码 public class Question_Activity_Adapter extends BaseAdapter {
private Activity activity;
private ArrayList<Question_Activity_Model> question_activity_modelArrayList;
private LayoutInflater inflater;
public Question_Activity_Adapter(Activity activity, ArrayList<Question_Activity_Model> question_activity_modelArrayList) {
this.activity = activity;
this.question_activity_modelArrayList = question_activity_modelArrayList;
this.inflater = activity.getLayoutInflater();
}
@Override
public int getCount() {
return question_activity_modelArrayList.size();
}
@Override
public Object getItem(int position) {
return question_activity_modelArrayList.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v= view;
if (v== null){
v= inflater.inflate(R.layout.questions_activity, null);
}
RadioGroup radioGroup= (RadioGroup) v.findViewById(R.id.radiogroup);
TextView questionTextView= (TextView) v.findViewById(R.id.textView2);
final RadioButton radioButton2= (RadioButton) v.findViewById(R.id.radioButton2);
final RadioButton radioButton3= (RadioButton) v.findViewById(R.id.radioButton3);
final RadioButton radioButton4= (RadioButton) v.findViewById(R.id.radioButton4);
final RadioButton radioButton5= (RadioButton) v.findViewById(R.id.radioButton5);
Question_Activity_Model question_activity_model= (Question_Activity_Model) getItem(i);
String question= question_activity_model.getQuestion();
questionTextView.setText(question);
final String option1= question_activity_model.getOption1();
radioButton2.setText(option1);
String option2= question_activity_model.getOption2();
radioButton3.setText(option2);
String option3= question_activity_model.getOption3();
radioButton4.setText(option3);
String option4= question_activity_model.getOption4();
radioButton5.setText(option4);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (radioButton2.isChecked()){
radioButton3.setEnabled(!isEnabled(i));
radioButton4.setEnabled(!isEnabled(i));
radioButton5.setEnabled(!isEnabled(i));
}
if (radioButton3.isChecked()){
radioButton2.setEnabled(!isEnabled(i));
radioButton4.setEnabled(!isEnabled(i));
radioButton5.setEnabled(!isEnabled(i));
}
if (radioButton4.isChecked()){
radioButton2.setEnabled(!isEnabled(i));
radioButton3.setEnabled(!isEnabled(i));
radioButton5.setEnabled(!isEnabled(i));
}
if (radioButton5.isChecked()){
radioButton2.setEnabled(!isEnabled(i));
radioButton3.setEnabled(!isEnabled(i));
radioButton4.setEnabled(!isEnabled(i));
}
}
});
return v;
}
}