Android getting the spinner id and name placed in the spinner - android

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

Related

update specific item at Android GridAdapter

I have an project where devices with Tasmota firmware are sending out MQTT JSON formated messages, that are used in my project to build JSONArray using .put method. Later the JSONArray changes are propagated to GridView adapter calling notifyDataSetChanged().
The challenge is to keep JSONArray structure same but with upto date (new MQTT messages arrives) data which arrive in different order then the initial JSONArray structure was. The result is that JSONArray embedded JSONObjects order changes on the fly and the gridView order changes too, that is confusing users. Temporally I tried to solve that by deleting latest JSON message from device that is sending the update from JSONArray by calling .remove and the re-adding updated Json Object calling .put method + calling notifyDataSetChanged() to reflect changes in the GridView.
//on new message arrived
sonoff_obj = mqtt.message;
for(json_position_index = 0; json_position_index < _arr_mqtt.length(); json_position_index++) {
if (_arr_mqtt.getJSONObject( json_position_index ).getString( "Topic" ).equals( _received_topic )){
_arr_mqtt.remove( json_position_index );
_arr_mqtt.put( sonoff_obj );
break;
}
}
Problem is now, that because using .put, then the new JsonObject is always added to last position in the JSONArray, also GridView layout gets reorganised, which confuses end users.
I am looking for solution how to update JsonObject in the JSONArray maintaining same index(position in the Array) or how to directly say the adapter to replace Item at specific position.
So far i can get adapter item content using .getItem( int index ); but can I tell the adapter something like in (my pseudo code) .setItem( int index , JsonObject obj) ?
For those who are facing the same challenge, ArrayList is much better choice than JsonArray, because it supports .set method which is with .notifyItemChanged() method powerful approach to modify specific position in the ArrayList. If you need for some reason to keep JSONArray format (for GridAdapter ie.), you can always export ArrayList into JSONArray like this (and of course call notifyDataSetChanged()):
my_mqtt_array = new JSONArray( my_arrayList.toString());
my_mqtt_array.notifyDataSetChanged();

could not obtain extracted text error on iterate an array

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

How to use multiple query.whereEqualTo multiple times with same key

I have a list of objectId ,I want to use these objectId to use these in recycler view. Below is the given code I am using to get my desired results
for (int i = 0; i < wishList.size(); i++) {
q2.whereEqualTo("objectId", wishList.get(i));
}
But the problem is only last index values of wishList is applying to my queries, my guess is previous index values are being overriden .
If any other solution please suggest (without using parse relation)
I think you can just do
q2.whereContainedIn("objectId", wishList)

How can i write these items for constructor in a for-loop?

so I'm currently writing an app for android, and im still a noob in java/android.
Anyways i have this 2 strings, one with names and the other with emails, and i want to output them in a listview with a custom adapter.
It works fine so far but i dont know how to set the items dynamically (with a for loop).
To create the adapter and so on, I used this tutorial:
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
I simply changed the ImageView to a second TextView.
In the tutorials code there are 5 items added to the list, but i need them dynamically, since Im not always having the same amount of name+emails to output
I already tried putting it in a for-loop by doing:
Weather weather_data[] = new Weather[names.length];
for(int z=0; z == names.length){
Weather[z]={new Weather(names[z], emails[z])};
}
I also tried it with adding "new" infront and trying to set everything null before, basically trial&error since i dont know much about it.
So can anyone tell me how I add the items dynamically?
(Ps: sorry if I used wrong names to describe anything)
This should work
Weather weather_data[] = new Weather[names.length];
for(int z=0; z < names.length; z++){
weather_data[z] = new Weather(names[z], emails[z]);
}
Give this a read to learn how for loops work
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
and this one for arrays
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
try this
ArrayList<Weather> weatherData = new ArrayList<>();
for (int i=0; i < names.length(); i++){
weatherData.add(new Weather(names[i], emails[i]));
}
Then when you need it as a Weather[] use weatherData.toArray()

Android JSONObject : Search function

I am trying to search from JSON code that has been received beforehand..
I was wondering are there any efficient way to search specific object?
Or should i use straight forward search like looping and if statement?
I have parsing the JSON successfully, and it stored in my String variable..
The JSON contains Array of Objects..
Now i want to search for specific ObjectName and get the Whole Objects..
The JSON will be like
[
{
"name":"blank",
"date":"2014-06-05T00:44:30Z",
"boolean":null,
},
{
"name":"hello",
"date":"2013-05-04T00:43:20Z",
"boolean":null,
}
]
I want to search for name = "hello" and get the last Array returned
Can anyone show me how?
Thanks a lot!
You can use something like this:
String name;
JSONObject searchObject;
JSONArray array = new JSONArray(yourJsonString);
for (int i = 0; i < array.length(); i++) {
JSONObject currObject = array.getJSONObject(i);
name = currObject.getString("name");
if(name == "hello")
{
searchObject = currObject
}
}
return searchObject
For big data you can't use search in every element imagin you have a 50K user you can't search in every 50k title to found your element I advise you to the backend - Web Developer - do this it will be easier to you and more faster how you can do this?
You just need a space to pass search text in JSON Url then post it and retrieve searched data from him
Try to use a web server with (PHP) and set your search element in GET like this.
Then the php search function will give you the full Json needed to your search. Also you can set pagination for this search like this.

Categories

Resources