could not obtain extracted text error on iterate an array - android

I'm beginner in Android and maybe my question be so simple.
I have defined a simple array and then push some strings from DB. like this :
db=new mydatabasehandlerTile(this);
mylist = new ArrayList<String>();
mylist=db.getfavoriteslist();
for(int i=1;i<=mylist.size();i++) {
Toast.makeText(this,String.valueOf(mylist.get(i)), Toast.LENGTH_SHORT).show();
}
As you can see I want to show each of array string item in Toast. but whenever I run my app I got this error and app is crashed :
onStartInput event aborted: com.touchtype.keyboard.h.p: could not obtain extracted text (class com.touchtype.keyboard.h.p)
I do not know what is problem. can anyone help me?

The obvious error that I see in your code is that you don't take in account that the index of the list is zero based, so change to this:
for(int i=0;i<mylist.size();i++) {
Toast.makeText(this,String.valueOf(mylist.get(i)), Toast.LENGTH_SHORT).show();
}

Related

Android - JSON throwing exception

So, I have a school project where I have to parse a json array and put the data in a listview.
Here is the json http://demo4404797.mockable.io/speakers
When I run the app, it shows only 5 elements, cause the fifth throws an exception ("org.json.JSONException: No value for Title", and I know the Title is missing. I just want to know to solve this) and the rest of the array elements arent read.
Here is the code:
I've read in other questions that we can use "ourobject.has("whatwewant")" but my teacher says it has to be done other way. Can you help me please?
The sixth item in the JSON you are using does not have the title key.
Since you are not allowed to use "ourobject.has("whatwewant")".
Just replace your code that uses speaker.getString() with this instead:
String name = speaker.optString("Name", "Name NA");
String image= speaker.optString("Image", "Image NA");
The opt method (there are several optString(), optLong(), optBoolean()...) will return the set value if it is available or it will return what ever value you put as a "fallback" if the key is not available.
Since you can't use the jsonObject.has("tag"), you can also break up the try-catch block.
String name;
try {
name = speaker.getString("name");
} catch (Exception e) {
name = ""; // Occurs when it can't find the tag.
}
Repeat this for every field. Please be intentional using this outside of the classroom.

Parse Query does not retrive me updated data from the cloud

I have an ParseObject in the cloud database. At the begining this object had an JSONArray that contained 2 JSONObjects. Then I run the next code:
Toast.makeText(getActivity(), "List lenght: " + new ParseQuery<MyParseObject>("MyParseObject").get("objectId").getJSONArray("MyJSONArray").length(), Toast.LENGTH_SHORT).show();
This show the result: "List lenght: 2". Well, the problem is when I manually delete a JSONObject from that List in the cloud, and later run the same code, the result is the same, but it should be: "List lenght: 1". I can see perfectly that there are only one JSONObject in the JSONArray.
What could happen?
Check once more your table, better on parse account site.
Seams to me, your field looks like [,object]. Thats why you have length of array = 2 but one of your object in array is empty, and you have only one valid JSON object.

Clearer Format Firebase Android retrieved data

When I retrieve my data it contains brackets { and unique id such as - JSDHGJDGJJSKA ... I want to make it cleaner and get rid of the brackets for e.g. my output is:
{-JfFQQRYnhiKeuN5ERGX={msg=Monday},-JfFQAhQQWIFAUuV1nD4={msg=this is test}}
I want to get rid of the brackets and the word msg and retrieve just one of the message at random.
I want my output to be if I pick up a random message:
Monday
if I pick up another at random
this is test
Any ideas on how to achieve this will be greatly appreciated.
This will retrieve a random message from the object you've shown in your question.
function getRandomMessage(data) {
if( !data ) { return null; }
var keys = Object.keys(data);
var randomKey = keys[ Math.floor(Math.random()*keys.length) ];
return data[randomKey];
}
Keep in mind that this assumes you have a small number of records. If you start getting into the thousands, you'll need a more robust solution than just grabbing the entire data set.
When I used this I was able to retrieve my data for eg. I save as Book -> title: "The book of death"
here is the code to retrieve the title:Retrieve data-
String title = (String) snapshot.child("title").getValue();
It worked after I used and I didnt used push since push creates its unique ID and its complex for my level to deal with it so I used:Saving data-
Map<String, Object> title= new HashMap<String, Object>();
title.put("title", "This is a working message");
f.child("Book").updateChildren(title);
and everything worked out. I hope it helps everyone who has having these issues. With update children you can use auto increment for your id.
are you getting data in string ? and If you are using string then it is easy , you can use the method of replace eg: yourString.replace("a","b")

Android getting the spinner id and name placed in the spinner

thank u for your time.
I was wondering if it is possible to get the id and name from a sql and place them both in the spinner.
with out creating a new AsyncTask and doInBackGround ect..
Witch will be active then onItemselected.
I have to little experience to make this decission.
With the JSON i have now working it only allows the ID or the NAME.
So i need to know if it is better to modify this JSON or create a new JSON with new php actions.
If i can get the confirmation to do this in the first this will save me time.
I am already 5 days at this problem.
Regards.
Nevermind i got it
private void loadSpinner(){
for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getName() + categoriesList.get(i).getId());
}
}

Converting ArrayList<T> to CharSequence[]

I want to convert my ArrayList containing elements with the toString method shadowed in "T":
public String toString(){
return name + " " + realname;
}
to a CharSequence array containing all the "T"s toString. Checked some stuff out but nothing works for me since CharSequence[] can't be concatenated (correct me if i'm wrong).
Saw a solution for the ArrayList at -> ArrayList<String> to CharSequence[] which didn't work out for me
I'm doing this because I've searched for devices ("T") and added them to a Arraylist, the user then has to make a choice which one to accept by clicking on a mutliple choice dialogwindow as shown under DIALOG_TEXT_ENTRY in this link
Please help me out cause it's driving me mad
So through the responses via the comments you guys suggested a solution with subsequence. However this is a similar solution I guess and it's semi-implemented in my code already so I'm gonna go with this one with a few modifications ofcourse.
Feel free to still give feedback if it's a bad way to implement or if there actually exists other ways of doing it!
::EDIT::
The solution was the following: I tried ot the code given by the link and take note that my ArrayList.toString() returns a String object with the following look for each element in the List
"[foo, bar, super, duper]"
Taken to account that the toString() is overriden in the my object class. The following is done
private String[] stringToArray(String str){
str = str.substring(1, str.length()-1);
String[] str2array = str.split(", ");
return str2array;
}
And voila I have the String[] array and can now represent them as choices in my single choice list.

Categories

Resources