android


how do I get the corresponding JSON value in my JSON object?


Getting the name value in my Toast in onItemClick of my ListView I can do, but how do I get the code value?
I have a JSON array that looks like this :
[
{
"code": "123",
"name": "Jim"
},
{
"code": "456",
"name": "Sally"
},
{
"code": "789",
"name": "Bob"
},
{
"code": "012",
"name": "Jill"
},
etc...
]
I get the name value in my ListView like this :
public void onStart(){
super.onStart();
// Create json array request
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST,
"http://www.nameofsite.com/nameoffile.php", (JSONArray)null, new Response.Listener<JSONArray>(){
public void onResponse(JSONArray jsonArray){
// Successfully download json
// So parse it and populate the listview
for(int i=0;i<jsonArray.length();i++){
try {
JSONObject jsonObject=jsonArray.getJSONObject(i);
items.add(jsonObject.getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Error", "Unable to parse json array");
}
});
// Create request queue
RequestQueue requestQueue= Volley.newRequestQueue(this);
// add json array request to the request queue
requestQueue.add(jsonArrayRequest);
}
And Toast of name in onItemClick like this :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView myTextView = (TextView) view.findViewById(R.id.txt);
String text = myTextView.getText().toString();
Toast.makeText(getApplicationContext(),text, Toast.LENGTH_LONG).show();
}
});
But I want the code value in the toast, not the name. How can I do it?
But I want the code value in the toast, not the name
Then you need to actually parse the "code" from the JSON rather than only "name", and add that to your adapter.
For example, parse it
String code = jsonObject.getString("code");
String name = jsonObject.getString("name");
items.add(code + " " + name);
And toast it
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String[] parts = items.get(i).split(" ");
Toast.makeText(getApplicationContext(),parts[0], Toast.LENGTH_LONG).show();
}
});
you can put name value to name arraylist and code to code arraylist , In onItemclick you will use this position to get code from code arraylist because it have same index . Or you can test with hashmap
Try this.
json.getJSONObject("jsonData").getString("name");

Related Links

How to fix styling of app
Render script rendering is much slower than OpenGL rendering on Android
opening another Android app in a 'new window' from Air
new fragment squeezing old fragment on add method
Galaxy Tab 3, turning on USB Debugging crashes USB driver
Listview image repeat from server in row
Create Single Menu(according to singleton pattern) for whole application in android
how to open database only once in a android project
Android certificate is expired
Google Play - Compability error on publish game made in Unity
Select only one radio button at a time in custom list view, android
Alert does not appear
What do I need to develop an Android chat app? On server,on client [closed]
how will I display the string that i getText() on the textview
Get meta-data from manifest in UnitTest
Android Bluetooth Client and Server Won't Connect

Categories

HOME
sonarqube
twitter-bootstrap-3
api
ng-idle
flex
camera
soa
emscripten
gembox-spreadsheet
bookmarks
webix
enthought
distribution
plink
altera
fileserver
production
sigma.js
solrcloud
asciidoctor-pdf
blade
spring-async
pimcore
aggregation
zxing
froala
android-sharedpreferences
jackson-modules
appirater
ms-access-2003
inversion-of-control
quantlib
corenlp-server
autodesk-designautomation
dotspatial
rselenium
login-script
least-squares
batching
engine.io
diacritics
p4merge
spring-mvc-test
news
kill
apriori
preloader
angular-http
android-checkbox
sonarlint-eclipse
google-cse
amq
wpas
azure-cli
avx
seq
trigger.io
donations
polyline
node-request
pure-css
atlas
microstation
flex4.6
dotnetnuke-7
mta
swizzling
deep
database-relations
jparsec
concept-insights
directdraw
morton-number
poppler
zen-cart
page.js
evolus-pencil
mirror
aspectj-maven-plugin
openbabel
filesplitting
contrast
webfinger
teaspoon
insight.database
wxformbuilder
conditionaltagsupport
c++builder-xe5
dtmf
cffile
nebula
windows-98
device-width
os.system
adobe-media-server
boost-signals
database-create
webdev.webserver
inline-formset
unattended-processing
qglwidget
updatesourcetrigger
easyb
strtod
complex-event-processing
pnrp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App