Android : How to use vector with xml parser in Android?

on Friday, October 10, 2014


I want to develop objective questions and answers in android.I have trying through xml parser I have add the values in textview and radio button like questions and 4 objectives i used radio buttons for this and two buttons for next and previous. When i run the application its working but when i select radio button(answers) and click next it goes to next question on the same Activity and after that when i go to previous question for changing the radio button(answers) at that time radio button is not selected on previously so how to know which answer i was selected due to this issue again i have to starting question but the question is start from where i have to exit the test but i want to start from first question again and again. Can someone help me out this.Thanks to appreciate.


Here is my assets/xxx.xml code



<?xml version="1.0" encoding="UTF-8"?>
<menu>
<item>
<QNO>8</QNO>
<QUESTIONS>Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?</QUESTIONS>
<ANSWER>java.util.Hashtable</ANSWER>
<OPTIONA>java.util.SortedMap</OPTIONA>
<OPTIONB>java.util.TreeMap</OPTIONB>
<OPTIONC>java.util.TreeSet</OPTIONC>
<OPTIOND>java.util.Hashtable</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>
<item>
<QNO>7</QNO>
<QUESTIONS>Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?</QUESTIONS>
<ANSWER>java.util.LinkedHashMap</ANSWER>
<OPTIONA>java.util.ArrayLi</OPTIONA>
<OPTIONB>java.util.HashMap</OPTIONB>
<OPTIONC>java.util.LinkedHashMap</OPTIONC>
<OPTIOND>java.util.TreeMap</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>
<item>
<QNO>6</QNO>
<QUESTIONS>Which interface provides the capability to store objects using a key-value pair?</QUESTIONS>
<ANSWER>Java.util.Map</ANSWER>
<OPTIONA>Java.util.Map</OPTIONA>
<OPTIONB>Java.util.Set</OPTIONB>
<OPTIONC>ava.util.List</OPTIONC>
<OPTIOND>Java.util.Collection</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>
<item>
<QNO>5</QNO>
<QUESTIONS>Which interface does java.util.Hashtable implement?</QUESTIONS>
<ANSWER>Java.util.Map</ANSWER>
<OPTIONA>Java.util.Map</OPTIONA>
<OPTIONB>Java.util.List</OPTIONB>
<OPTIONC>Java.util.HashTable</OPTIONC>
<OPTIOND>Java.util.Collection</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>
<item>
<QNO>4</QNO>
<QUESTIONS>You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?</QUESTIONS>
<ANSWER>java.util.Set</ANSWER>
<OPTIONA>ava.util.Map</OPTIONA>
<OPTIONB>ava.util.Set</OPTIONB>
<OPTIONC>java.util.List</OPTIONC>
<OPTIOND>java.util.Collection</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>
<item>
<QNO>3</QNO>
<QUESTIONS>Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?</QUESTIONS>
<ANSWER>ava.util.ArrayList</ANSWER>
<OPTIONA>java.util.HashSet</OPTIONA>
<OPTIONB>java.util.LinkedHashSet</OPTIONB>
<OPTIONC>java.util.List</OPTIONC>
<OPTIOND>ava.util.ArrayList</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>
<item>
<QNO>2</QNO>
<QUESTIONS>Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?</QUESTIONS>
<ANSWER>java.lang.StringBuffer</ANSWER>
<OPTIONA>java.lang.String</OPTIONA>
<OPTIONB>java.lang.Double</OPTIONB>
<OPTIONC>java.lang.StringBuffer</OPTIONC>
<OPTIOND>java.lang.Character</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>

<item>
<QNO>1</QNO>
<QUESTIONS>Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?</QUESTIONS>
<ANSWER>LinkedHashMap</ANSWER>
<OPTIONA>TreeMap</OPTIONA>
<OPTIONB>HashMap</OPTIONB>
<OPTIONC>LinkedHashMap</OPTIONC>
<OPTIOND>The answer depends on the implementation of the existing instance.</OPTIOND>
<EXPLAINATION>NONE</EXPLAINATION>
<QUESTIONTYPE>Java Keyword</QUESTIONTYPE>
</item>
</menu>


Here is xml parser



public class SAXXMLParser
{
static Context context;

public SAXXMLParser()
{
}


public static Vector parse(InputStream inputstream) throws SAXException, ParserConfigurationException, IOException
{

Vector vector1;
XMLReader xmlreader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
SAXXMLHandler saxxmlhandler = new SAXXMLHandler();
xmlreader.setContentHandler(saxxmlhandler);

xmlreader.parse(new InputSource(inputstream));
vector1 = saxxmlhandler.getDatabase();
Vector vector = vector1;

System.out.println((new StringBuilder("Vectore Length:::::::")).append(vector.size()).toString());
Test_Demo.Vectore_mquestionDatabaseStructure = vector;
return vector;
}
}


Here is xmlhandler code



public class SAXXMLHandler extends DefaultHandler
{

private Vector datastructure;
private Datastructure tempEmp;
private String tempVal;

public SAXXMLHandler()
{
datastructure = new Vector();
}

public void characters(char ac[], int i, int j)
throws SAXException
{
tempVal = new String(ac, i, j);
}

public void endElement(String s, String s1, String s2)
throws SAXException
{
if (s2.equalsIgnoreCase("item"))
{
datastructure.add(tempEmp);
} else
{
if (s2.equalsIgnoreCase("QNO"))
{
tempEmp.setQNo(tempVal);
System.out.println((new StringBuilder("AAAAAAAAAAAA:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("QUESTIONS"))
{
tempEmp.setQuestion(tempVal);
System.out.println((new StringBuilder("BBBBBBBBBBB:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("ANSWER"))
{
tempEmp.setAnswer(tempVal);
System.out.println((new StringBuilder("Answer")).append(tempVal).toString());
System.out.println((new StringBuilder("CCCCCCCCCCCCCCCCC:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("OPTIONA"))
{
tempEmp.setOptionA(tempVal);
System.out.println((new StringBuilder("DDDDDDDDDD:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("OPTIONB"))
{
tempEmp.setOptionB(tempVal);
System.out.println((new StringBuilder("EEEEEEEEEEEEEE:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("OPTIONC"))
{
tempEmp.setOptionC(tempVal);
System.out.println((new StringBuilder("FFFFFFFFFFFFFF:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("OPTIOND"))
{
tempEmp.setOptionD(tempVal);
System.out.println((new StringBuilder("GGGGGGGGG:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("EXPLAINATION"))
{
tempEmp.setExplanation(tempVal);
System.out.println((new StringBuilder("HHHHHHHHHHH:::::")).append(tempVal).toString());
return;
}
if (s2.equalsIgnoreCase("QUESTIONTYPE"))
{
tempEmp.setQuestion_Type(tempVal);
System.out.println((new StringBuilder("IIIIIIIIIIIII:::::")).append(tempVal).toString());
return;
}
}
}

public Vector getDatabase()
{
return datastructure;
}

public void startElement(String s, String s1, String s2, Attributes attributes)
throws SAXException
{
tempVal = "";
if (s2.equalsIgnoreCase("item"))
{
tempEmp = new Datastructure();
}
}
}


Here is my


read_Questions_XML_File();



button_Previouse.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
previousQuestionCalled(v);
}
});


button_Next.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

nextQuestionCalled(v);
}
});

button_Home.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iBack = new Intent(Test_Demo.this , Java_Lesson.class);
iBack.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
iBack.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(iBack);
finish();
}
});

}

public void storingResult()
{
int selectedId = Options_RadioGroup.getCheckedRadioButtonId();
String strOption_Id = String.valueOf(selectedId);
System.out.println("storingResult() strOption_Id = " + strOption_Id);

AnswerOptions = (RadioButton) findViewById(Options_RadioGroup.getCheckedRadioButtonId());
String str_AnswerOptions = AnswerOptions.getText().toString().trim();
System.out.println("rbVal = " + str_AnswerOptions);

if (str_AnswerOptions.equals(((Datastructure) Vectore_mquestionDatabaseStructure .get(StaticClass.QuestionNumber)).Answer))
{
if (!StaticClass.isTest)
{
String str_queNo = button_QuestionLimit.getText()
.toString().trim();
String strstrqueType = txtViewQuestiontype.getText()
.toString().trim();
String str_que = txtViewQuestion.getText().toString()
.trim();
String str_marks = "1";

databaseHelper.insertQueDetails(str_queNo, strstrqueType, str_que, str_AnswerOptions, str_marks , strOption_Id);
Toast.makeText(getApplicationContext()," Data Crated Succesfully ",Toast.LENGTH_SHORT).show();
}
}

if (StaticClass.isTest)
{
StaticClass.resultOfTest = 1 + StaticClass.resultOfTest;
((Datastructure)
Vectore_mquestionDatabaseStructure.elementAt(StaticClass.QuestionNumber)).setResult_Of_Test(true);
return;
}
else
{
if (!StaticClass.isTest)

{
String str_queNo = button_QuestionLimit.getText()
.toString().trim();
String strstrqueType = txtViewQuestiontype.getText()
.toString().trim();
String str_que = txtViewQuestion.getText().toString()
.trim();
String str_marks = "0";

databaseHelper.insertQueDetails(str_queNo, strstrqueType, str_que,
str_AnswerOptions, str_marks , strOption_Id);

Toast.makeText(getApplicationContext()," Data Crated Wrong Succesfully
",Toast.LENGTH_SHORT).show();
}

if (StaticClass.isTest)
{
StaticClass.resultOfTest = 1 + StaticClass.resultOfTest;
((Datastructure) Vectore_mquestionDatabaseStructure
.elementAt(StaticClass.QuestionNumber))
.setResult_Of_Test(true);
return;
}
}


}

public void nextQuestionCalled(View view)
{

int id = Options_RadioGroup.getCheckedRadioButtonId();
System.out.println("id = " + id);
if (id > 0)
{
storingResult();
if (animationForward)
{
boolean flag;
if (animationForward) { flag = false; }

else { flag = true; }
animationForward = flag;
}

if (-1 + Vectore_mquestionDatabaseStructure.size() > StaticClass.QuestionNumber)
{
if (StaticClass.isTest)
{
StaticClass.resultOfTest_Out_OF = 1 + StaticClass.resultOfTest_Out_OF;
}

StaticClass.QuestionNumber = 1 + StaticClass.QuestionNumber;
reHitting();
view.clearAnimation();

if (!StaticClass.isTest) { button_QuestionLimit.clearAnimation(); }

if (StaticClass.isTest) { button_QuestionLimit.clearAnimation(); }

return;
}
}

else if (id == -1)
{ Toast.makeText(this, "Please Select Any Option", Toast.LENGTH_LONG).show(); }

if( vector_Size == convertVector)
{

Intent i = new Intent(Test_Demo.this , Submit_Test_Page.class);
startActivity(i);
finish();
}

Options_RadioGroup.clearCheck();
}


public void syoutF()
{
addVallues();
}

public void previousQuestionCalled(View view)
{
Options_RadioGroup.check(2131165204);
if (!animationForward)
{
boolean flag;

if (animationForward)
{
flag = false;
}
else
{
flag = true;
}
animationForward = flag;
}
if (StaticClass.QuestionNumber > 0)
{
if (!StaticClass.isTest)
{
StaticClass.QuestionNumber = -1 + StaticClass.QuestionNumber;
reHitting();
button_QuestionLimit.clearAnimation();
}
}

else
{
button_QuestionLimit.startAnimation(mAnimation);
}

}



public void reHitting()
{
addVallues();
if (!animationForward)
{
overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
return;
}
else
{
overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
return;
}
}

public void addVallues()
{
try
{
Options_RadioGroup.clearCheck();

txtViewQuestion.setText(((Datastructure) Vectore_mquestionDatabaseStructure
.get(StaticClass.QuestionNumber)).Question);

AnswerOptionsA_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
.get(StaticClass.QuestionNumber)).OptionA);

AnswerOptionsB_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
.get(StaticClass.QuestionNumber)).OptionB);

AnswerOptionsC_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
.get(StaticClass.QuestionNumber)).OptionC);

AnswerOptionsD_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
.get(StaticClass.QuestionNumber)).OptionD);

QuestionLimitFetcher();


return;
}

catch (ArrayIndexOutOfBoundsException arrayindexoutofboundsexception) {
arrayindexoutofboundsexception.printStackTrace();
}
}


public void QuestionLimitFetcher()
{
int i = Vectore_mquestionDatabaseStructure.size();
button_QuestionLimit.setText((new StringBuilder(String.valueOf(1 +
StaticClass.QuestionNumber))).append("/").append(i).toString());

strQueLimit = String.valueOf( 1 + StaticClass.QuestionNumber );
convertVector = Integer.parseInt(strQueLimit);
System.out.println("strQueLimit = " + strQueLimit);
System.out.println("convertVector = " + convertVector);

}

public void read_Questions_XML_File()
{
try
{

encodedXML = URLEncoder.encode("features_java.xml");
db = SAXXMLParser.parse(getAssets().open(encodedXML));
vector_Size = Vectore_mquestionDatabaseStructure.size();
System.out.println("Vector Size = " + vector_Size);
}
catch (IOException ioexception)
{
ioexception.printStackTrace();
}

catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
syoutF();
}

}

0 comments:

Post a Comment