Return a Name corrosponding to its ID from JSON object Android












1















I have an array of JSON objects for example



"data": {
"id": 20,
"name" : "Car"
},
"data": {
"id": 2,
"name" : "Bus"
},
"data": {
"id": 30,
"name" : "Bike"
}


I am getting these values from database and storing them in a shared preference



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
SharedPreferences.Editor vehicleEditor = vehicleData.edit();

vID.append(vt.get(i).getId()).append(",");
vName.append(vt.get(i).getTypeName()).append(",");


I have a spinner in my android app where I am showing the values as "Car", "Bus", "Bike" etc (from the shared preference).



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
String ids = vehicleData.getString("vehicle_type_id", "");
final String names = vehicleData.getString(ids, "");

String singleName = names.split(",");
String singleID = ids.split(",");

typeSpinner = findViewById(R.id.vehicle_type_spinner);

ArrayList<String> namelist = new ArrayList<>();

for (int i = 0; i < singleName.length; i++) {
namelist.add(singleID[i].concat(" ".concat(singleName[i])));
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, namelist);
typeSpinner.setAdapter(adapter);


and getting the spinner value in this way



String typeName = String.valueOf(typeSpinner.getSelectedItem());


What I want to do is when a user selects a value from the spinner (Ex: Car), I want to store the ID of that value i.e 1 in a variable.



I guess this is a very basic question but I cannot solve it. How can I do this?










share|improve this question

























  • What is your current code looks like?

    – Emre Savcı
    Nov 28 '18 at 20:30











  • Currently its a mess. What I do is first I get a string of both name and id in a comma separated string ("Car, Bus, Bike" and "1, 2, 3"), then I split each of them at comma, and run a for loop through and form a concatenated string something like "1 Car". In doing to i get the spinner values as the same "1 Car". Then i again separate them to get the ID when a snipper is selected. Like I said its a mess.

    – Mill3r
    Nov 28 '18 at 20:47











  • You have the json values as serialized java object right? So you can search on that list object which has the name equals to selected value as I understand. Am I missing something?

    – Emre Savcı
    Nov 28 '18 at 20:49













  • actually I am getting these values from database and storing them in shared preference in another activity as a key value pair ("id", 1) and ("name", car). Something like this. i will edit my question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:53
















1















I have an array of JSON objects for example



"data": {
"id": 20,
"name" : "Car"
},
"data": {
"id": 2,
"name" : "Bus"
},
"data": {
"id": 30,
"name" : "Bike"
}


I am getting these values from database and storing them in a shared preference



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
SharedPreferences.Editor vehicleEditor = vehicleData.edit();

vID.append(vt.get(i).getId()).append(",");
vName.append(vt.get(i).getTypeName()).append(",");


I have a spinner in my android app where I am showing the values as "Car", "Bus", "Bike" etc (from the shared preference).



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
String ids = vehicleData.getString("vehicle_type_id", "");
final String names = vehicleData.getString(ids, "");

String singleName = names.split(",");
String singleID = ids.split(",");

typeSpinner = findViewById(R.id.vehicle_type_spinner);

ArrayList<String> namelist = new ArrayList<>();

for (int i = 0; i < singleName.length; i++) {
namelist.add(singleID[i].concat(" ".concat(singleName[i])));
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, namelist);
typeSpinner.setAdapter(adapter);


and getting the spinner value in this way



String typeName = String.valueOf(typeSpinner.getSelectedItem());


What I want to do is when a user selects a value from the spinner (Ex: Car), I want to store the ID of that value i.e 1 in a variable.



I guess this is a very basic question but I cannot solve it. How can I do this?










share|improve this question

























  • What is your current code looks like?

    – Emre Savcı
    Nov 28 '18 at 20:30











  • Currently its a mess. What I do is first I get a string of both name and id in a comma separated string ("Car, Bus, Bike" and "1, 2, 3"), then I split each of them at comma, and run a for loop through and form a concatenated string something like "1 Car". In doing to i get the spinner values as the same "1 Car". Then i again separate them to get the ID when a snipper is selected. Like I said its a mess.

    – Mill3r
    Nov 28 '18 at 20:47











  • You have the json values as serialized java object right? So you can search on that list object which has the name equals to selected value as I understand. Am I missing something?

    – Emre Savcı
    Nov 28 '18 at 20:49













  • actually I am getting these values from database and storing them in shared preference in another activity as a key value pair ("id", 1) and ("name", car). Something like this. i will edit my question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:53














1












1








1








I have an array of JSON objects for example



"data": {
"id": 20,
"name" : "Car"
},
"data": {
"id": 2,
"name" : "Bus"
},
"data": {
"id": 30,
"name" : "Bike"
}


I am getting these values from database and storing them in a shared preference



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
SharedPreferences.Editor vehicleEditor = vehicleData.edit();

vID.append(vt.get(i).getId()).append(",");
vName.append(vt.get(i).getTypeName()).append(",");


I have a spinner in my android app where I am showing the values as "Car", "Bus", "Bike" etc (from the shared preference).



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
String ids = vehicleData.getString("vehicle_type_id", "");
final String names = vehicleData.getString(ids, "");

String singleName = names.split(",");
String singleID = ids.split(",");

typeSpinner = findViewById(R.id.vehicle_type_spinner);

ArrayList<String> namelist = new ArrayList<>();

for (int i = 0; i < singleName.length; i++) {
namelist.add(singleID[i].concat(" ".concat(singleName[i])));
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, namelist);
typeSpinner.setAdapter(adapter);


and getting the spinner value in this way



String typeName = String.valueOf(typeSpinner.getSelectedItem());


What I want to do is when a user selects a value from the spinner (Ex: Car), I want to store the ID of that value i.e 1 in a variable.



I guess this is a very basic question but I cannot solve it. How can I do this?










share|improve this question
















I have an array of JSON objects for example



"data": {
"id": 20,
"name" : "Car"
},
"data": {
"id": 2,
"name" : "Bus"
},
"data": {
"id": 30,
"name" : "Bike"
}


I am getting these values from database and storing them in a shared preference



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
SharedPreferences.Editor vehicleEditor = vehicleData.edit();

vID.append(vt.get(i).getId()).append(",");
vName.append(vt.get(i).getTypeName()).append(",");


I have a spinner in my android app where I am showing the values as "Car", "Bus", "Bike" etc (from the shared preference).



SharedPreferences vehicleData = getSharedPreferences("vehicleData", Context.MODE_PRIVATE);
String ids = vehicleData.getString("vehicle_type_id", "");
final String names = vehicleData.getString(ids, "");

String singleName = names.split(",");
String singleID = ids.split(",");

typeSpinner = findViewById(R.id.vehicle_type_spinner);

ArrayList<String> namelist = new ArrayList<>();

for (int i = 0; i < singleName.length; i++) {
namelist.add(singleID[i].concat(" ".concat(singleName[i])));
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, namelist);
typeSpinner.setAdapter(adapter);


and getting the spinner value in this way



String typeName = String.valueOf(typeSpinner.getSelectedItem());


What I want to do is when a user selects a value from the spinner (Ex: Car), I want to store the ID of that value i.e 1 in a variable.



I guess this is a very basic question but I cannot solve it. How can I do this?







java android arrays json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 20:55







Mill3r

















asked Nov 28 '18 at 20:03









Mill3rMill3r

131315




131315













  • What is your current code looks like?

    – Emre Savcı
    Nov 28 '18 at 20:30











  • Currently its a mess. What I do is first I get a string of both name and id in a comma separated string ("Car, Bus, Bike" and "1, 2, 3"), then I split each of them at comma, and run a for loop through and form a concatenated string something like "1 Car". In doing to i get the spinner values as the same "1 Car". Then i again separate them to get the ID when a snipper is selected. Like I said its a mess.

    – Mill3r
    Nov 28 '18 at 20:47











  • You have the json values as serialized java object right? So you can search on that list object which has the name equals to selected value as I understand. Am I missing something?

    – Emre Savcı
    Nov 28 '18 at 20:49













  • actually I am getting these values from database and storing them in shared preference in another activity as a key value pair ("id", 1) and ("name", car). Something like this. i will edit my question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:53



















  • What is your current code looks like?

    – Emre Savcı
    Nov 28 '18 at 20:30











  • Currently its a mess. What I do is first I get a string of both name and id in a comma separated string ("Car, Bus, Bike" and "1, 2, 3"), then I split each of them at comma, and run a for loop through and form a concatenated string something like "1 Car". In doing to i get the spinner values as the same "1 Car". Then i again separate them to get the ID when a snipper is selected. Like I said its a mess.

    – Mill3r
    Nov 28 '18 at 20:47











  • You have the json values as serialized java object right? So you can search on that list object which has the name equals to selected value as I understand. Am I missing something?

    – Emre Savcı
    Nov 28 '18 at 20:49













  • actually I am getting these values from database and storing them in shared preference in another activity as a key value pair ("id", 1) and ("name", car). Something like this. i will edit my question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:53

















What is your current code looks like?

– Emre Savcı
Nov 28 '18 at 20:30





What is your current code looks like?

– Emre Savcı
Nov 28 '18 at 20:30













Currently its a mess. What I do is first I get a string of both name and id in a comma separated string ("Car, Bus, Bike" and "1, 2, 3"), then I split each of them at comma, and run a for loop through and form a concatenated string something like "1 Car". In doing to i get the spinner values as the same "1 Car". Then i again separate them to get the ID when a snipper is selected. Like I said its a mess.

– Mill3r
Nov 28 '18 at 20:47





Currently its a mess. What I do is first I get a string of both name and id in a comma separated string ("Car, Bus, Bike" and "1, 2, 3"), then I split each of them at comma, and run a for loop through and form a concatenated string something like "1 Car". In doing to i get the spinner values as the same "1 Car". Then i again separate them to get the ID when a snipper is selected. Like I said its a mess.

– Mill3r
Nov 28 '18 at 20:47













You have the json values as serialized java object right? So you can search on that list object which has the name equals to selected value as I understand. Am I missing something?

– Emre Savcı
Nov 28 '18 at 20:49







You have the json values as serialized java object right? So you can search on that list object which has the name equals to selected value as I understand. Am I missing something?

– Emre Savcı
Nov 28 '18 at 20:49















actually I am getting these values from database and storing them in shared preference in another activity as a key value pair ("id", 1) and ("name", car). Something like this. i will edit my question for better understanding.

– Mill3r
Nov 28 '18 at 20:53





actually I am getting these values from database and storing them in shared preference in another activity as a key value pair ("id", 1) and ("name", car). Something like this. i will edit my question for better understanding.

– Mill3r
Nov 28 '18 at 20:53












1 Answer
1






active

oldest

votes


















2














I hope this piece of code can help you :



private void initSpinner() {
List<Data> listOfData = new ArrayList<>();
listOfData.add(new Data(1,"Car"));
listOfData.add(new Data(2,"Bus"));
listOfData.add(new Data(3,"Bike"));

List<String> listOfNames = new ArrayList<>();
for (Data data : listOfData) {
listOfNames.add(data.getName());
}

spinner.setItems(listOfNames);
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
long selectedDataId = listOfData.get(position);
String selectedDataName = listOfNames.get(position);
}
});
}





share|improve this answer
























  • I think you are trying to get the position of each value. I have tried this way, but its not full prove in my case as by id can be different than its position. check my edited question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:50











  • No , i just create tow type of lists , first list for class data and the seconde is for names , when you click in choosen name you get the position of this name and you passe it into list of class to get the id of this name

    – Sofiane Majdoub
    Nov 28 '18 at 21:06











  • oh. I see. let me try this.

    – Mill3r
    Nov 28 '18 at 21:07











  • So the idea is to get the position of the id in the id array. and get the name at that position in the name array.

    – Mill3r
    Dec 3 '18 at 7:54











  • yes , that's i want to explain , you put the string array of names into the spinner after that when you click in the item you get the position of this name and you use it the get the id from other the array .

    – Sofiane Majdoub
    Dec 3 '18 at 9:04












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527280%2freturn-a-name-corrosponding-to-its-id-from-json-object-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I hope this piece of code can help you :



private void initSpinner() {
List<Data> listOfData = new ArrayList<>();
listOfData.add(new Data(1,"Car"));
listOfData.add(new Data(2,"Bus"));
listOfData.add(new Data(3,"Bike"));

List<String> listOfNames = new ArrayList<>();
for (Data data : listOfData) {
listOfNames.add(data.getName());
}

spinner.setItems(listOfNames);
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
long selectedDataId = listOfData.get(position);
String selectedDataName = listOfNames.get(position);
}
});
}





share|improve this answer
























  • I think you are trying to get the position of each value. I have tried this way, but its not full prove in my case as by id can be different than its position. check my edited question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:50











  • No , i just create tow type of lists , first list for class data and the seconde is for names , when you click in choosen name you get the position of this name and you passe it into list of class to get the id of this name

    – Sofiane Majdoub
    Nov 28 '18 at 21:06











  • oh. I see. let me try this.

    – Mill3r
    Nov 28 '18 at 21:07











  • So the idea is to get the position of the id in the id array. and get the name at that position in the name array.

    – Mill3r
    Dec 3 '18 at 7:54











  • yes , that's i want to explain , you put the string array of names into the spinner after that when you click in the item you get the position of this name and you use it the get the id from other the array .

    – Sofiane Majdoub
    Dec 3 '18 at 9:04
















2














I hope this piece of code can help you :



private void initSpinner() {
List<Data> listOfData = new ArrayList<>();
listOfData.add(new Data(1,"Car"));
listOfData.add(new Data(2,"Bus"));
listOfData.add(new Data(3,"Bike"));

List<String> listOfNames = new ArrayList<>();
for (Data data : listOfData) {
listOfNames.add(data.getName());
}

spinner.setItems(listOfNames);
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
long selectedDataId = listOfData.get(position);
String selectedDataName = listOfNames.get(position);
}
});
}





share|improve this answer
























  • I think you are trying to get the position of each value. I have tried this way, but its not full prove in my case as by id can be different than its position. check my edited question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:50











  • No , i just create tow type of lists , first list for class data and the seconde is for names , when you click in choosen name you get the position of this name and you passe it into list of class to get the id of this name

    – Sofiane Majdoub
    Nov 28 '18 at 21:06











  • oh. I see. let me try this.

    – Mill3r
    Nov 28 '18 at 21:07











  • So the idea is to get the position of the id in the id array. and get the name at that position in the name array.

    – Mill3r
    Dec 3 '18 at 7:54











  • yes , that's i want to explain , you put the string array of names into the spinner after that when you click in the item you get the position of this name and you use it the get the id from other the array .

    – Sofiane Majdoub
    Dec 3 '18 at 9:04














2












2








2







I hope this piece of code can help you :



private void initSpinner() {
List<Data> listOfData = new ArrayList<>();
listOfData.add(new Data(1,"Car"));
listOfData.add(new Data(2,"Bus"));
listOfData.add(new Data(3,"Bike"));

List<String> listOfNames = new ArrayList<>();
for (Data data : listOfData) {
listOfNames.add(data.getName());
}

spinner.setItems(listOfNames);
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
long selectedDataId = listOfData.get(position);
String selectedDataName = listOfNames.get(position);
}
});
}





share|improve this answer













I hope this piece of code can help you :



private void initSpinner() {
List<Data> listOfData = new ArrayList<>();
listOfData.add(new Data(1,"Car"));
listOfData.add(new Data(2,"Bus"));
listOfData.add(new Data(3,"Bike"));

List<String> listOfNames = new ArrayList<>();
for (Data data : listOfData) {
listOfNames.add(data.getName());
}

spinner.setItems(listOfNames);
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
long selectedDataId = listOfData.get(position);
String selectedDataName = listOfNames.get(position);
}
});
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 20:44









Sofiane MajdoubSofiane Majdoub

11117




11117













  • I think you are trying to get the position of each value. I have tried this way, but its not full prove in my case as by id can be different than its position. check my edited question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:50











  • No , i just create tow type of lists , first list for class data and the seconde is for names , when you click in choosen name you get the position of this name and you passe it into list of class to get the id of this name

    – Sofiane Majdoub
    Nov 28 '18 at 21:06











  • oh. I see. let me try this.

    – Mill3r
    Nov 28 '18 at 21:07











  • So the idea is to get the position of the id in the id array. and get the name at that position in the name array.

    – Mill3r
    Dec 3 '18 at 7:54











  • yes , that's i want to explain , you put the string array of names into the spinner after that when you click in the item you get the position of this name and you use it the get the id from other the array .

    – Sofiane Majdoub
    Dec 3 '18 at 9:04



















  • I think you are trying to get the position of each value. I have tried this way, but its not full prove in my case as by id can be different than its position. check my edited question for better understanding.

    – Mill3r
    Nov 28 '18 at 20:50











  • No , i just create tow type of lists , first list for class data and the seconde is for names , when you click in choosen name you get the position of this name and you passe it into list of class to get the id of this name

    – Sofiane Majdoub
    Nov 28 '18 at 21:06











  • oh. I see. let me try this.

    – Mill3r
    Nov 28 '18 at 21:07











  • So the idea is to get the position of the id in the id array. and get the name at that position in the name array.

    – Mill3r
    Dec 3 '18 at 7:54











  • yes , that's i want to explain , you put the string array of names into the spinner after that when you click in the item you get the position of this name and you use it the get the id from other the array .

    – Sofiane Majdoub
    Dec 3 '18 at 9:04

















I think you are trying to get the position of each value. I have tried this way, but its not full prove in my case as by id can be different than its position. check my edited question for better understanding.

– Mill3r
Nov 28 '18 at 20:50





I think you are trying to get the position of each value. I have tried this way, but its not full prove in my case as by id can be different than its position. check my edited question for better understanding.

– Mill3r
Nov 28 '18 at 20:50













No , i just create tow type of lists , first list for class data and the seconde is for names , when you click in choosen name you get the position of this name and you passe it into list of class to get the id of this name

– Sofiane Majdoub
Nov 28 '18 at 21:06





No , i just create tow type of lists , first list for class data and the seconde is for names , when you click in choosen name you get the position of this name and you passe it into list of class to get the id of this name

– Sofiane Majdoub
Nov 28 '18 at 21:06













oh. I see. let me try this.

– Mill3r
Nov 28 '18 at 21:07





oh. I see. let me try this.

– Mill3r
Nov 28 '18 at 21:07













So the idea is to get the position of the id in the id array. and get the name at that position in the name array.

– Mill3r
Dec 3 '18 at 7:54





So the idea is to get the position of the id in the id array. and get the name at that position in the name array.

– Mill3r
Dec 3 '18 at 7:54













yes , that's i want to explain , you put the string array of names into the spinner after that when you click in the item you get the position of this name and you use it the get the id from other the array .

– Sofiane Majdoub
Dec 3 '18 at 9:04





yes , that's i want to explain , you put the string array of names into the spinner after that when you click in the item you get the position of this name and you use it the get the id from other the array .

– Sofiane Majdoub
Dec 3 '18 at 9:04




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53527280%2freturn-a-name-corrosponding-to-its-id-from-json-object-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)