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

String comparison not working in android [duplicate]
What is difference between Android memory management and Linux memory management? [closed]
How to create GridView image gallery programmatically in android?
Android NullPointerException while retrieveing data
How to get the intent filter of an android app
Classes to test aren't within scope of test package
Can I use Linux system calls in android?
Devices Layout [Android]
URLClassLoader working on Java but not in Android
How to know if the application is in foreground?
Android class diagram and XML
Prestashop and web service(Restful), HttpURLConnection, BufferedReader
Assign text read from file in network to TextView in Android fragment
How to execute TranslateAnimation sequentially without knowing how many translate you'll have to do (android)?
Android MediaCodec issue on Alcatel OneTouch 7025D
FATAL EXCEPTION: java.lang.RuntimeException: takePicture failed

Categories

HOME
uml
notepad++
deep-learning
linkedin
redux-form
nunit
mstest
oracle-apex-5
solver
error-handling
emscripten
code-coverage
lanczos
joomla3.0
okhttp
reactive-programming
heap
spring-social
endpoint
do-while
azure-powershell
smartclient
breadth-first-search
poedit
easeljs
scriptella
expressionengine3
interbase
pylons
asymptotic-complexity
errbot
carrierwave
sqlpackage
alm
magento-1.8
hibernate-validator
git-squash
getpixel
yowsup
spring-mvc-test
trigonometry
microsoft-metro
ssrs-2014
bindingnavigator
angular-http
qtranslate
backbone-collections
bbpress
lsyncd
pocketsphinx-android
builtins
iotivity
tinkerpop3
dotnetnuke-7
lua-telegram-bot
riot
bizagi
aurora
html-to-pdf
crouton-os
jbase
wiredep
sha512
liquid-layout
diff3
cakephp-2.7
phpldapadmin
java-security
variadic-templates
uncaught-typeerror
browserify-shim
imagefilter
jama
unix-socket
functional-java
wxformbuilder
eclipse-kepler
ie8-compatibility-mode
kaleidoscope
android-icons
booksleeve
midlet
vfs
web-farm
cakephp-1.2
dajaxice
multiplexing
isa-swizzling
wiimote
virtualpathprovider
automationelement
maven-1
office-2007
rowlex
tui
linfu-dynamicproxy
flvplayback
dsoframer

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