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

Fatal signal 6 (sigabrt) (code=-6) webview
Check Bluetooth version in Android
Delphi Video freezes
Create Surface View With Child View
Trouble integrating FB login into my android app
How to make search widget larger in actionbar?
Android Layout Design Specific Custom
Generic Asynctask for multiple activities
ListView OnItemClickListener doesn't fire
Get access to particular item's layout in list view
Android: OTG Storage notification conflicts with radio c
Layout looks different on devices
Check android device density in html
Is that possible to connect two phones with adb connect?
App using Mobile Android GNSK crashes when identifyAlbumAsync() is called before audioProcessStart()
Get values from multiple RadioGroups

Categories

HOME
phantom-dsl
search
assembly
angular-formly
youtube-data-api-v3
sh
devise
tvos
android-json
iis-6
data-visualization
mvc5
crystal-lang
performancepoint
okhttp
arangodb
java.util.logging
intercom
mongodb-csharp
capistrano3
azure-iot-hub
sse
boost-icl
asp.net-core-webapi
replication
microsoft-ocr
ember-cli
gpio
entity-relationship-model
apktool
slacktextviewcontroller
batching
video-capture
connection-pool
metaprogramming
comparison-operators
plaintext
microsoft-metro
plr
optimizely
facebook-pixel
openblas
x-ray
dt
flyout
py2neo
objectscript
cd-burning
bootstrap-tags-input
rsa-archer-grc
custom-lists
builtins
latin
preprocessor
spring-cloud-feign
jodd
i386
scaffolding
dup
angular-ui-typeahead
cda
boost-serialization
beanstalk
morton-number
google-books
ons-api
finalbuilder
bcache
easyrtc
renderer
solace-mq
sphinxql
windows-phone-8-sdk
ice-cube
facebook-timeline
http-status-code-410
twisted.web
sentestingkit
mapping-by-code
matlab-load
winmain
quotation-marks
codebase
delimited-text
qtabbar
strtod
wcf-instancing
online-storage

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