Vikas Kumar Vikas Kumar - 5 months ago 6
Android Question

Getting error in the app after making custom fragments

I have made a quiz app in which I have stored the questions in database and then taken out from the database in the form of list and I am making custom fragments to upload the questions from the arraylist but I am getting only 1 question on every fragment

This is my database code

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 = "quest1";
// 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("Face","Small forehead", "Copper coloured with plenty of pimples", "Attractive good looking");
this.addQuestion(q1);
Question q2=new Question("Body weight", "Light", "Medium", "Heavy");
this.addQuestion(q2);
Question q3=new Question("Body structure","Lean, tall", "Medium sized","Heavy, short");
this.addQuestion(q3);
Question q4=new Question("Body skin", "Dry, cough", "Soft and warm", "Soft, Glossy, Cold, Clear");
this.addQuestion(q4);
Question q5=new Question("Body strength","Weak","Moderately Strong","Strong");
this.addQuestion(q5);
Question q6=new Question("Blood vessels","Prominent, reticulated","Less prominent with greenish colour","Not at all visible");
this.addQuestion(q6);
Question q7=new Question("Body temperature","Slightly less than normal","Slightly above normal","Normal");
this.addQuestion(q7);
Question q8=new Question("Eyes","Protuberant, small, eye lashes scanty","Sharp, brown/copper/red coloured eye lashes","Large attractive thick eye brows");
this.addQuestion(q8);
Question q9=new Question("Teeth","Cracker, irregular, dull white","Moderate sized yellow","Large white strong");
this.addQuestion(q9);
Question q10=new Question("Nails","Rough, dry less growth","Glossy copper coloured","Long white strong");
this.addQuestion(q10);
Question q11=new Question("Lips","Dry thin","Red, soft moist","Thick, glossy");
this.addQuestion(q11);
Question q12=new Question("Hands","Short","Medium","Long");
this.addQuestion(q12);
Question q13=new Question("Body hairs","Scanty","Few brownish","Thick dark plenty");
this.addQuestion(q13);
Question q14=new Question("Hairs on scalp","Brown rough dry","Scanty tendency to early baldness","Plenty thick");
this.addQuestion(q14);
Question q15=new Question("Long joints","Prominent flabby","Flabby","Strong well formed");
this.addQuestion(q15);
Question q16=new Question("Abdomen","Thin","Normal","Large");
this.addQuestion(q16);

Question q17=new Question("Chest","Thin narrow","Medium","Wide strong well covered with muscle");
this.addQuestion(q17);
Question q18=new Question("Pulse","Irregular, quick","Fast pace, volume is less","Slow, full in volume");
this.addQuestion(q18);
Question q19=new Question("Body smell","None","Foul smell","Oily smell");
this.addQuestion(q19);
Question q20=new Question("Foot prints","Ill defined","Normal","Well defined");
this.addQuestion(q20);
Question q21=new Question("Tongue","Dark spot","Copper coloured, thin","Long white strong");
this.addQuestion(q21);
Question q22=new Question("Performance of activities","Quickly with a lot of initiative","Moderately medium initiative","Very slowly");
this.addQuestion(q22);
Question q23=new Question("Excitability","Quickly","Very Quickly","Slowly");
this.addQuestion(q23);
Question q24=new Question("Power to grasp","Very fast","Quick ","Long");
this.addQuestion(q24);
Question q25=new Question("Memory","Very short","Medium","Long");
this.addQuestion(q25);
Question q26=new Question("Digestive power","Irregular","Very powerful","Weak");
this.addQuestion(q26);
Question q27=new Question("Ability to bear hunger","Sometimes good, sometimes poor","Poor ","Good");
this.addQuestion(q27);
Question q28=new Question("Quantity of food","Sometimes large, sometimes poor","Always large","Normal");
this.addQuestion(q28);
Question q29=new Question("Groups of tastes desired","Sweet, Salt, Sour","Sweet, Bitter, Astringent Pungent, Astringent, Bitter","Pungent, Astringent, Bitter");
this.addQuestion(q29);

Question q30=new Question("Thirst","Irregular","Very often","Seldom");
this.addQuestion(q30);
Question q31=new Question("Food desired","Warm Food","Cold Food","Warm and Dry Food");
this.addQuestion(q31);
Question q32=new Question("Bowel movements","Irregular","Loose stools","Regular, well-formed stool");
this.addQuestion(q32);
Question q33=new Question("Perspiration","Seldom","Very easily with foul body odour","Normal");
this.addQuestion(q33);
Question q34=new Question("Sleep","Sleep Interrupted and less","Normal","Excessive and deep");
this.addQuestion(q34);
Question q35=new Question("Dreams","Flying, jumping, climbing hills","Violence, flight and struggle","Water body, cloud, romance");
this.addQuestion(q35);
Question q36=new Question("Attitude to problems","Lot of worry, instability","Angry, easily provoked and highly irritable","Peaceful slow steady");
this.addQuestion(q36);
Question q37=new Question("Talk","Very fast missing words","Sharp, clear-cut provocative","Slow resonant, clear");
this.addQuestion(q37);
Question q38=new Question("Associated body movements","Moves les shoulder","Normal","No associated movements");
this.addQuestion(q38);


}
@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;
}
}


and this is my question.java class

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
) {

QUESTION = qUESTION;
OPTA = oPTA;
OPTB = oPTB;
OPTC = oPTC;

}
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;
}

}


and this is my custom fragment code

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;
}
}


and this my main activity.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>();
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.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC());
fragments.add(FirstFragment.newInstance(page, currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
}

/* fragments.add(FirstFragment.newInstance(0,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//1
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(1,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//2
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(2,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//3
// currentQ = quesList.get(qid);
/* fragments.add(FirstFragment.newInstance(3,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//4
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(4,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//5
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(5,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//6
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(6,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//7
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(7,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//8
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(8,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//9
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(9,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//10
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(10,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//11
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(11,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//12
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(12,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//13
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(13,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//14
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(14,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//15
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(15,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//16
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(16,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//17
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(17,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//18
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(18,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//19
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(19,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//20
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(20,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//21
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(21,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//22
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(22,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//23
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(23,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//24
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(24,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//25
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(25,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//26
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(26,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//27
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(27,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//28
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(28,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//29
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(29,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//30
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(30,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//31
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(31,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//32
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(32,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//33
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(33,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//34
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(34,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//35
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(35,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//36
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(36,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//37
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(37,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;//38
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(38,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);
}
}


and after this when the app is being launched it is giving unfortunately your app has stopped so please tell the changes which I should do help me please fast thanks in advance

Answer

This is because you have an error when you are trying to add the questions. You are adding the same questions again and again.

Change the below code :

 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()));

to

 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()));