我已经制作了一个测验应用程序,我将问题存储在数据库中,然后以列表的形式从数据库中取出,我正在制作自定义片段来上传arraylist的问题,但我只收到1个问题在每个片段上
这是我的数据库代码
package com.example.android.viewpager4;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Question q1=new Question("What is JP?","Jalur Pesawat", "Jack sParrow", "Jasa Programmer", "Jasa Programmer");
this.addQuestion(q1);
Question q2=new Question("where the JP place?", "Monas, Jakarta", "Gelondong, Bangun Tapan, bantul", "Gelondong, Bangun Tapan, bandul", "Gelondong, Bangun Tapan, bantul");
this.addQuestion(q2);
Question q3=new Question("who is CEO of the JP?","Usman and Jack", "Jack and Rully","Rully and Usman", "Rully and Usman" );
this.addQuestion(q3);
Question q4=new Question("what do you know about JP?", "JP is programmer home", "JP also realigy home", "all answer is true","all answer is true");
this.addQuestion(q4);
Question q5=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
this.addQuestion(q5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
values.put(KEY_OPTC, quest.getOPTC());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quest.setOPTC(cursor.getString(5));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
这是我的问题.java类
package com.example.android.viewpager4;
public class Question {
private int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String OPTC;
private String ANSWER;
public Question()
{
ID=0;
QUESTION="";
OPTA="";
OPTB="";
OPTC="";
ANSWER="";
}
public Question(String qUESTION, String oPTA, String oPTB, String oPTC,
String aNSWER) {
QUESTION = qUESTION;
OPTA = oPTA;
OPTB = oPTB;
OPTC = oPTC;
ANSWER = aNSWER;
}
public int getID()
{
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getOPTA() {
return OPTA;
}
public String getOPTB() {
return OPTB;
}
public String getOPTC() {
return OPTC;
}
public String getANSWER() {
return ANSWER;
}
public void setID(int id)
{
ID=id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setOPTA(String oPTA) {
OPTA = oPTA;
}
public void setOPTB(String oPTB) {
OPTB = oPTB;
}
public void setOPTC(String oPTC) {
OPTC = oPTC;
}
public void setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}
这是我的自定义片段代码
package com.example.android.viewpager4;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.List;
/**
* Created by Vikas on 6/19/2016.
*/
public class FirstFragment extends Fragment {
// Store instance variables
private String question;
private String radio_1,radio_2,radio_3;
private int page;
List<Question> quesList;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
// newInstance constructor for creating fragment with arguments
public static FirstFragment newInstance(int page, String question,String radio_1,String radio_2,String radio_3)
{
FirstFragment fragmentFirst = new FirstFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("some_question", question);
args.putString("some_option1",radio_1);
args.putString("some_option1",radio_2);
args.putString("some_option1",radio_3);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
DbHelper db = new DbHelper(getContext());
quesList = db.getAllQuestions(); //get all the questions from database
//getting the id of the question
currentQ = quesList.get(qid);
page = getArguments().getInt("someInt", 0);
question = getArguments().getString("some_question",currentQ.getQUESTION());
radio_1=getArguments().getString("some_option1", currentQ.getOPTA());
radio_2=getArguments().getString("some_option2", currentQ.getOPTB());
radio_3=getArguments().getString("some_option3", currentQ.getOPTC());
qid++;
}
// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
TextView question_view = (TextView) view.findViewById(R.id.question_id);
question_view.setText(question);
rda=(RadioButton)view.findViewById(R.id.radioButton1);
rdb=(RadioButton)view.findViewById(R.id.radioButton2);
rdc=(RadioButton)view.findViewById(R.id.radioButton3);
rda.setText(radio_1);
rdb.setText(radio_2);
rdc.setText(radio_3);
return view;
}
}
这是我的主要活动.java
package com.example.android.viewpager4;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.List;
import java.util.Vector;
public class MainActivity extends AppCompatActivity
{
List<Question> quesList;
int qid=0;
Question currentQ;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout);
initialisePaging();
}
private void initialisePaging()
{
DbHelper db = new DbHelper(this);
quesList = db.getAllQuestions(); //get all the questions from database
//getting the id of the question
currentQ = quesList.get(qid);
List<FirstFragment> fragments =new Vector<FirstFragment>();
fragments.add(FirstFragment.newInstance(0,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
fragments.add(FirstFragment.newInstance(1,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
fragments.add(FirstFragment.newInstance(2,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
mPagerAdapter = new com.example.android.viewpager4.PagerAdapter(this.getSupportFragmentManager(),fragments);
ViewPager pager =(ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
}
}
这是我的pageadapter.java
package com.example.android.viewpager4;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Vikas on 4/27/2016.
*/
public class PagerAdapter extends FragmentPagerAdapter
{
private List<FirstFragment> fragments;
public PagerAdapter(FragmentManager fm, List<FirstFragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int arg0)
{
return this.fragments.get(arg0);
}
@Override
public int getCount()
{
return this.fragments.size();
}
}
并且在整个代码之后我在每个片段中得到相同的问题请告诉我应该做的更改请帮助我,请提前快速致谢
答案 0 :(得分:1)
这是因为您在尝试添加问题时出错。您是一次又一次地添加相同的问题。
更改以下代码:
currentQ = quesList.get(qid);
List<FirstFragment> fragments =new Vector<FirstFragment>();
fragments.add(FirstFragment.newInstance(0,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
fragments.add(FirstFragment.newInstance(1,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
fragments.add(FirstFragment.newInstance(2,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
到
currentQ = quesList.get(qid);
List<FirstFragment> fragments =new Vector<FirstFragment>();
fragments.add(FirstFragment.newInstance(0,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(1,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(2,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
答案 1 :(得分:0)
我认为你的问题源于这三行,因为所有这些键都是相同的。
args.putString("some_option1",radio_1);
args.putString("some_option1",radio_2);
args.putString("some_option1",radio_3);
一些建议,尝试重新构建适配器填充代码,如此
quesList = db.getAllQuestions();
for (int page = 0; page < quesList.size(); page++) {
Question currentQ = quesList.get(page);
// Is that page parameter needed in here?
FirstFragment questionFragment = FirstFragment.newInstance(page, currentQ);
fragments.add(questionFragment);
}
mPagerAdapter = new com.example.android.viewpager4.PagerAdapter(this.getSupportFragmentManager(),fragments);
请注意currentQ
中的newInstance
看起来比currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()
但是,为了防止抛出Fragment对象列表,您可以更改Adapter以获取问题列表。
public class QuestionPagerAdapter extends FragmentPagerAdapter
{
private List<Question> questions;
public PagerAdapter(FragmentManager fm, List<Question> questions) {
super(fm);
this.questions = questions;
}
@Override
public Fragment getItem(int position)
{
Question q = questions.get(position);
// Is that int parameter needed here?
return FirstFragment.newInstance(position, q);
}
@Override
public int getCount()
{
return this.questions.size();
}
}
在这两种情况下,您都需要更新newInstance
方法。
public static FirstFragment newInstance(int page, Question q)
{
FirstFragment fragmentFirst = new FirstFragment();
Bundle args = new Bundle();
args.putInt("someInt", page); // Is this needed?
args.putString("some_question", q.getQUESTION());
args.putString("some_option1",q.getOPTA());
args.putString("some_option2",q.getOPTB());
args.putString("some_option3",q.getOPTC());
fragmentFirst.setArguments(args);
return fragmentFirst;
}
然后,为了使该方法看起来更清晰,您可以为这些参数字符串创建一些public static final String
字段,就像您为数据库键所做的那样。
例如
public static final String ARG_QUESTION = "some_question";
public static final String ARG_OPT_A = "some_option1";
// etc.
然后
args.putString(ARG_QUESTION, q.getQUESTION());
args.putString(ARG_OPT_A, q.getOPTA());
// etc.
并且
Bundle args = getArguments();
if (args != null)
{
question = args.getString(ARG_QUESTION, currentQ.getQUESTION());
radio_1 = args.getString(ARG_OPT_A, currentQ.getOPTA());
// etc.
}
最后,制作Question
实施Parcelable
将是将数据传递到片段的最佳方法,这样,您就不需要重新获得currentQ
超出数据库。 (因为您正在读取整个数据库,并且仅在缺少这些参数时默认为qid
0)。