How to choose a specific element from an XML file? - android

I make a small test-program for my child. I want to learn his math.
Faced with the problem of reading the question from the XML file
<?xml version="1.0" encoding="utf-8"?>
<questions>
<question>
<vopros>How much will 2+2?</vopros>
<otvet_1>2</otvet_1>
<otvet_2>6</otvet_2>
<otvet_3>8</otvet_3>
<otvet_4 name="pravilno">4</otvet_4>
</question>
<question>
<vopros>How much will 3+3?</vopros>
<otvet_1>12</otvet_1>
<otvet_2>16</otvet_2>
<otvet_3>18</otvet_3>
<otvet_4 name="pravilno">6</otvet_4>
</question>
<question>
<vopros>How much is 4+4?</vopros>
<otvet_1>22</otvet_1>
<otvet_2>26</otvet_2>
<otvet_3>18</otvet_3>
<otvet_4 name="pravilno">8</otvet_4>
</question>
<question>
<vopros>How much is 5+5?</vopros>
<otvet_1>2</otvet_1>
<otvet_2>6</otvet_2>
<otvet_3>8</otvet_3>
<otvet_4 name="pravilno">10</otvet_4>
</question>
</questions>
I need to activate vopros3 and answer options (otvet_1, otvet_2,otvet_3,otvet_4)
Java code that retrieves data from an XML file
try {
XmlPullParser parser = getResources().getXml(R.xml.voprosi);
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() == XmlPullParser.START_TAG
&& parser.getName().equals("question")) {
if(numvopr==3)
{
// ....... here it is necessary to withdraw a question and answers
Question.setText(...);
answ1.setText(...);
answ2.setText(...);
answ3.setText(...);
answ4.setText(...);
}
numvopr++;
}
parser.next();
}
} catch (Throwable t) {
Toast.makeText(this,
"XML Error: " + t.toString(),
Toast.LENGTH_LONG).show();
}

One way to implement as a state machine:
String lastStartTag = "";
// change to desired question - 0-based
int targetQuestion = 3;
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
// Count questions and record every START tag
if (parser.getEventType() == XmlPullParser.START_TAG) {
lastStartTag = parser.getName();
if (parser.getName().equals("question")) {
numvopr++;
}
}
// Process text based on whether we are at desired question
// and last processed START tag.
else if (parser.getEventType() == XmlPullParser.TEXT) {
if (numvopr == targetQuestion) {
if (lastStartTag.compareTo("vopros") == 0) {
Question.setText(parser.getText());
}
else if (lastStartTag.compareTo("otvet_1") == 0) {
answ1.setText(parser.getText());
}
else if (lastStartTag.compareTo("otvet_2") == 0) {
answ2.setText(parser.getText());
}
else if (lastStartTag.compareTo("otvet_3") == 0) {
answ3.setText(parser.getText());
}
else if (lastStartTag.compareTo("otvet_4") == 0) {
answ4.setText(parser.getText());
}
}
}
parser.next();
}

A slightly different implementation with switch statement.
int numOfQuestions = 0;
String text = null;
try {
XmlPullParser parser = getResources().getXml(R.xml.test);
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
String elementName = parser.getName();
switch (parser.getEventType()) {
case XmlPullParser.START_TAG:
if ("question".equals(elementName)) {
numOfQuestions++;
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (numOfQuestions == 3) {
switch (elementName) {
case "vopros":
Log.i(TAG, "Question: " + text);
break;
case "otvet_1":
Log.i(TAG, "answ1: " + text);
break;
case "otvet_2":
Log.i(TAG, "answ2: " + text);
break;
case "otvet_3":
Log.i(TAG, "answ3: " + text);
break;
case "otvet_4":
Log.i(TAG, "answ4: " + text);
break;
default:
break;
}
}
break;
default:
break;
}
parser.next();
}
} catch (Throwable t) {
Log.e(TAG, "Problem while parsing the xml.", t);
}

Related

How to change background of button on next click

Currently I have a list of TextViews whose background get changed when click for the first time. But I also want when user clicks on the second time its background should again change and vice versa.
With my existing code I am only able to change background for the first click, when I am clicking it again its background is not changing.
Here is my code:
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (count <= 2) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText2:
if (count <= 2) {
goals.add(mGoal2.getText().toString());
mGoal2.setTextColor(getResources().getColor(R.color.black));
mGoal2.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText3:
if (count <= 2) {
goals.add(mGoal3.getText().toString());
mGoal3.setTextColor(getResources().getColor(R.color.black));
mGoal3.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText4:
if (count <= 2) {
goals.add(mGoal4.getText().toString());
mGoal4.setTextColor(getResources().getColor(R.color.black));
mGoal4.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText5:
if (count <= 2) {
goals.add(mGoal5.getText().toString());
mGoal5.setTextColor(getResources().getColor(R.color.black));
mGoal5.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText6:
if (count <= 2) {
goals.add(mGoal6.getText().toString());
mGoal6.setTextColor(getResources().getColor(R.color.black));
mGoal6.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText7:
if (count <= 2) {
goals.add(mGoal7.getText().toString());
mGoal7.setTextColor(getResources().getColor(R.color.black));
mGoal7.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnGoal:
Intent intent = new Intent(this, fiteness_level_selection.class);
try {
obj.put("SelectedGoal", goals);
} catch (Exception e) {
e.printStackTrace();
}
intent.putExtra("GoalJson", obj.toString());
startActivity(intent);
break;
}
So can anybody suggest me any easy way to achieve it.
Set a boolean array in your activity.
//suppose you've 3 textviews within listview
public boolean isTransparent[] = {false,false,false};
And then do this in your onClick method.
public void onOnclick(View v){
switch (v.getId()) {
//foreach textview do this
case R.id.textView1 :
int color;
if (isTransparent[0])
color = getResources().getColor(android.R.color.transparent);
else
color = getResources().getColor(R.color.white);
isTransparent[0]=!isTransparent[0];
textview1.setBackgroundColor(color);
break;
case R.id.textView2:
// now check for isTransparent[1]
and so on ...
...
}
}
As What I am understand is you want Change background for that you have take bool variable and in Button click you have to check the bool value.
Boolean click_firsttime = false;
Inside your Button click
use this way
If(click_firsttime == false){
//perform task
// and set bool value to true
click_firsttime = true;
}else{
// perform task and set bool value
click_firsttime = false;
}
First create a boolean :
private boolean foo = true;
Then change your code like this :
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (foo) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
foo = false;
} else {
// set background to another color
foo = true;
}
break;

How to display volume slider with Chromecast icon when not using remoteMediaPlayer

I am trying to implement the volume slider while casting and show the cast icon in the slider like in the Youtube app : http://imgur.com/kVxoKbM.
As of now I have overridden the dispatchKeyEvent function my activity, as explained in the cast developers documentation (shown in the code snipped below), which allows me to control the volume of the connected Chromecast perfectly. But on the device, it doesn't even show a slider at all.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
if (mApiClient != null) {
double currentVolume = Cast.CastApi.getVolume(mApiClient);
if (currentVolume < 1.0) {
try {
Cast.CastApi.setVolume(mApiClient,
Math.min(currentVolume + VOLUME_INCREMENT, 1.0));
} catch (Exception e) {
Log.e(TAG, "unable to set volume", e);
}
}
} else {
Log.e(TAG, "dispatchKeyEvent - volume up");
}
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
if (mApiClient != null) {
double currentVolume = Cast.CastApi.getVolume(mApiClient);
if (currentVolume > 0.0) {
try {
Cast.CastApi.setVolume(mApiClient,
Math.max(currentVolume - VOLUME_INCREMENT, 0.0));
} catch (Exception e) {
Log.e(TAG, "unable to set volume", e);
}
}
} else {
Log.e(TAG, "dispatchKeyEvent - volume down");
}
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
I am not using the remoteMediaPlayer. So how can I implement a volume slider while controlling the connected Chromecast volume?

How works the method PlusClient getCurrentLocation()?

I'm trying recover the location of the class PlusClient, but this method always returns NULL.
In this question the user Lee say:
Have you set your location to be publicly visible? It will only be
returned if you have made it public.
But I tested with the location public and continues return NULL, I try get my location too, and the return is NULL too.
Below my code:
public void onPeopleLoaded(ConnectionResult status, PersonBuffer personBuffer,
String nextPageToken) {
switch (status.getErrorCode()) {
case ConnectionResult.SUCCESS:
mListItems.clear();
try {
int count = personBuffer.getCount();
for (int i = 0; i < count; i++) {
mListItems.add(personBuffer.get(i).getDisplayName()+"\n"+
((personBuffer.get(i).hasCurrentLocation()) ?
personBuffer.get(i).getCurrentLocation() :
"Value NULL"));
}
} finally {
personBuffer.close();
}
mListAdapter.notifyDataSetChanged();
break;
case ConnectionResult.SIGN_IN_REQUIRED:
Log.d(TAG, "Reconectando");
mPlusClient.disconnect();
mPlusClient.connect();
break;
default:
Log.e(TAG, "Error when listing people: " + status);
break;
}
}
I'm doing anything wrong?

XmlPullParser with base64encode tag

I am parsing xml packet using XmlPullParser. I am able to parse base64encode tag. My issue here is "I am not able to read full base64encode data. Only part of it is able to read".
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
String elementName = parser.getName();
String namespace = parser.getNamespace();
if(elementName.equals("vCard") && namespace.equals("vcard-temp"))
{
}
// Otherwise, see if there is a registered provider for
// this element name and namespace.
else {
Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
if (provider != null) {
if (provider instanceof IQProvider) {
iqPacket = ((IQProvider)provider).parseIQ(parser);
}
else if (provider instanceof Class) {
iqPacket = (IQ)PacketParserUtils.parseWithIntrospection(elementName,
(Class)provider, parser);
}
}
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("iq")) {
done = true;
}
}else if(eventType == XmlPullParser.TEXT){
String xx = parser.getText();
System.out.println("binaval "+ xx);
}
}
very long string as a response of web service
see this answer here sometimes for the very long string it doesn't work mine was the same case when I posted this question.

Android Getting the GPSStatus always return nothing, why?

my code is:
android.location.GpsStatus.Listener gpsstatusListenerGps = new android.location.GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
GpsStatus gpsStatus = lm.getGpsStatus(null);
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
locationResult.gotSatellites(gpsStatus);
Log.e("gpsStatus", "gpsStatus");
lm.removeGpsStatusListener(gpsstatusListenerGps);
for (GpsSatellite sat:lm.getGpsStatus(null).getSatellites()) {
Log.e("STATUS", "gpsx.GpsStatus.Sat.fixed: " + sat.usedInFix());
}
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
locationResult.gotSatellites(gpsStatus);
Log.e("FIX", "gpsx.fixed.");
lm.removeGpsStatusListener(gpsstatusListenerGps);
for (GpsSatellite sat:lm.getGpsStatus(null).getSatellites()) {
Log.e("FIXX", "gpsx.GpsStatus.Sat.fixed: " + sat.usedInFix());
}
break;
}
}
};
...
Log.e("sat.hasNext()",""+sat.hasNext());
while (sat.hasNext()){
GpsSatellite oSat = (GpsSatellite) sat.next();
Log.e("gps",""+oSat.getSnr());
}
sat.hasNext is always false.
i always getting nullstring, so i cant get gpsstatuses, why?

Categories

Resources