I have yet another question regarding displaying image on listview from json object. I checked almost every possible answers on stackoverflow but did not find any solution. I have following implementation
contact = json.getJSONArray(TAG_CONTACT);
for(int i = 0; i < contact.length(); i++){
JSONObject c = contact.getJSONObject(i);
//put json obkject on variable
String fname = c.getString(TAG_NAME);
String uname = c.getString(TAG_UNAME);
String type = c.getString(TAG_TYPE);
String email = c.getString(TAG_EMAIL);
String thumb = c.getString(TAG_THUMB);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, fname);
map.put(TAG_UNAME, uname);
map.put(TAG_TYPE, type);
map.put(TAG_EMAIL, email);
map.put(TAG_THUMB, thumb);
contactlist.add(map);
// Finding the list view
list=(ListView)findViewById(R.id.contactlist);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// Put data on listview content
ListAdapter adapter = new SimpleAdapter(ContactPage.this, contactlist,
R.layout.contact_list,
new String[] {TAG_NAME,TAG_UNAME,TAG_TYPE,TAG_EMAIL,TAG_THUMB}, new int[] {
R.id.contact_name,R.id.contact_usertype,R.id.contact_status,R.id.contact_email,R.id.contact_image});
list.setAdapter(adapter);
Now where and how shall I implement the imgLoader.DisplayImage(url, imageView) in my code if I have image URL on thumb , i.e c.getString(TAG_THUMB);
Thanks in advance.
Related
There's someway for me to adapt a encoded string image in a Listview?
I got from my server, in base64 format, a image in a string, and I want to set it in a Listview with some text.
Here is part of my code:
for (int i = 0; i < makers.length(); i++) {
JSONObject c = makers.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String age = c.getString(TAG_AGE);
String username = c.getString(TAG_USERNAME);
String rating = c.getString(TAG_RATING);
String disc = c.getString(TAG_DISC);
Long date = c.getLong(TAG_DATE);
String num = c.getString(TAG_NUM);
// creating new HashMap
//HashMap<String, String> map = new HashMap<String, String>();
Map map = new HashMap<String, Bitmap>();
String image=c.getString(TAG_IMAGE1);
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
map.put(TAG_NAME, name);
map.put(TAG_USERNAME, username);
map.put(TAG_AGE, age);
map.put(TAG_RATING, rating);
map.put(TAG_DISC, disc);
map.put(TAG_NUM, num);
map.put(TAG_IMAGE1, bitmap);
// map.put(TAG_CIDADE, cidade);
long curdate = System.currentTimeMillis();
long onem = curdate - date;
long intm = 3*30*24*60*60*1000 ;
if(onem >= intm){
} else {
// adding HashList to ArrayList
userdataList.add((HashMap<String, String>) map);
}
}
}
And here is my Adapter:
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting the related idioms
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ListResult1.this, userdataList,
R.layout.list_view, new String[] { TAG_NAME, TAG_USERNAME, TAG_AGE, TAG_RATING, TAG_DISC, TAG_NUM, TAG_IMAGE1},
new int[] {R.id.name, R.id.username, R.id.age, R.id.rating, R.id.disc, R.id.num, R.id.ivimage1s});
// updating listview
setListAdapter(adapter);
}
});
}
And i want also to transfer that image through a intent, it's possible?
The actual problem is that I can't set a adapter for my image, all the text part work just fine, but the image did not loaded.
Sorry for any English issue, it's not my mother language.
I parse json data and display it in listview with CHOICE_MODE_MULTIPLE. I trying to get the key of a Hashmap in my case but I only manage to get the value.
Json data
57->Steak Doness
58->Size
59->Cooking Method
63->Coldness
Here is the coding
String option_data = jsonObject.getString("option_data");
JSONObject jsonObject1 = new JSONObject(option_data);
Iterator<String> iterator = jsonObject1.keys();
ArrayList arrayList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = null;
while (iterator.hasNext()) {
String id = iterator.next();
String name = jsonObject1.getString(id);
map = new HashMap<String, String>();
map.put("id", id);
map.put("name", name);
arrayList.add(name);
}
ArrayAdapter adapter = new ArrayAdapter<String>(item_add.this, android.R.layout.simple_list_item_multiple_choice, arrayList);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String selected = "";
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getCount(); i++) {
if (sparseBooleanArray.get(i)) {
selected += listView.getItemAtPosition(i).toString() + ",";
}
}
Log.d("selected", selected);
}
});
With the logcat, it shows
D/selected: Steak Doness,Size,
However, what I want to get is the Key in Hashmap which means "id" when click the button . Anyone knows how to do it?
Use the following way to iterate and find values of keys
HashMap <String,String> mapOne= new HashMap();
mapOne.put("1","1value");
mapOne.put("2","2value");
mapOne.put("3","3value");
for(Map.Entry<String, String> mapEntry : mapOne.entrySet()){
if (mapEntry.getValue().equals("3value")) {
//System.out.println(mapEntry.getKey());
Log.d("selected", mapEntry.getKey());
}
}
I'm building a a Gallery app using GridView and ViewPager. I'm getting the image URLs from JSON. I've got the GridView displaying the images correctly, and now I'm moving on to the ViewPager. What I think I need to do is generate a String array of the image URLs for the ViewPager Adapter. In the GridView Activity, I've created a HashMap with the id, URL, and image description strings stored. My question now is: Is it possible to generate a String Array by retrieving all the stored URL strings from the HashMap? What I need is this from the HashMap:
public static final String[] imagesStr = new String[] {
"http://www.mysite/images/building0001.jpg",
"http://www.mysite/images/building00011.jpg",
"http://www.mysite/images/building0010.jpg" };
Here's how I'm generating the HashMap:
#Override
protected Void doInBackground(Void... params) {
// Retrieve JSON Objects from the given URL address
galleryArrList = new ArrayList<HashMap<String, String>>();
jsonobject = JIJSONfunctions.getJSONfromURL(urlPathStr);
try {
// Locate the array name in JSON
JSArrGallery = jsonobject.getJSONArray("gallery");
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
idStr = galleryJO.getString(TAG_ID);
urlStr = galleryJO.getString(TAG_URL);
descrStr = galleryJO.getString(TAG_DESCR);
// creating new HashMap
HashMap<String, String> map = new HashMap<String,String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, idStr);
map.put(TAG_URL, urlStr);
map.put(TAG_DESCR, descrStr);
galleryArrList.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
I'm a bit unclear on how a HashMap works, but I assume that the code is going through the JSON Array and pulling each URL string out and storing it in the HashMap, right? So now can I pull all the gathered URL Strings and create a String Array with them? How would I do that?
Seems straightforward enough. After you have populated your List<HashMap<String,String>>, iterate over each map and pull the value you need from it to add to a String[]. Please refer to the following snippet:
galleryArrList = new ArrayList<HashMap<String, String>>();
jsonobject = JIJSONfunctions.getJSONfromURL(urlPathStr);
try { ... }
final int length = galleryArrList.size();
final String[] imagesStr = new String[length];
for (int i = 0; i < length; i++) {
final Map<String, String> map = galleryArrList.get(i);
imagesStr[i] = map.get(TAG_URL);
}
You might try using an ArrayList and add the URLs to the ArrayList within the for loop. When the loop is done, you can use the ArrayList.toArray(T[] array) method copy the URLs from the ArrayList to a String array.
ArrayList.toArray
ArrayList arlist = new ArrayList();
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
idStr = galleryJO.getString(TAG_ID);
urlStr = galleryJO.getString(TAG_URL);
descrStr = galleryJO.getString(TAG_DESCR);
// creating new HashMap
HashMap<String, String> map = new HashMap<String,String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, idStr);
map.put(TAG_URL, urlStr);
map.put(TAG_DESCR, descrStr);
galleryArrList.add(map);
arlist.add(urlStr);
}
// Create a String array to hold the URLs
String[] arUrls = new String[arlist.size()];
// Copy the URLs from the ArrayList to the String array
arlist.toArray(arUrls);
Sometime json array appears with image url's and sometimes it is empty. I am using the following code where array with image url displaying data and image in listview but when array is empty then data is also not displaying. How can i slove this? Any answer will be appreciated
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
JSONArray posts = json.getJSONArray(KEY_POSTS);
// looping through all song nodes <song>
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String title = c.getString(KEY_TITLE);
String date = c.getString(KEY_DATE);
String content = c.getString(KEY_CONTENT);
//authornumber is agin JSON Object
JSONObject author = c.getJSONObject(KEY_AUTHOR);
String name = author.getString(KEY_NAME);
JSONArray atta = c.getJSONArray("attachments");
for(int j = 0; j < atta.length(); j++){
JSONObject d = atta.getJSONObject(j);
String slug = d.getString(KEY_SLUG);
JSONObject images = d.getJSONObject(KEY_IMAGES);
JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
String url = thumbnail.getString(KEY_URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_TITLE, title);
map.put(KEY_DATE, date);
map.put(KEY_NAME, name);
map.put(KEY_CONTENT, content);
map.put(KEY_SLUG, slug);
map.put(KEY_URL, url);
// adding HashList to ArrayList
songsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing json data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
i need to set a listview with custom text and custom setbackgroundresource when i create the listview
This is my code:
The listView receive the data from URL by JSON encdoe like that:
{"results":[
{"db_id":"6","discount":"active","db_description":"bla bla bla ","db_num":"137","db_num2":"260"},
{"db_id":"14","db_type":"discount","db_description":"blaaaaaaa","db_num":"39","db_num2":"46"},
{"db_id":"18","db_type":"discount","db_description":"blaaaaaaa","db_num":"335","db_num2":"456"},
]}
Adding the data to map
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("type",RESULTS_PARAMS));
JSONObject json = jsonParser.makeHttpRequest(RESULTS_URL, "GET",
params);
Log.d("JSON RESULT: ", json.toString());
try {
queues = json.getJSONArray(TAG_RESULTS);
for (int i = 0; i < queues.length(); i++) {
JSONObject c = queues.getJSONObject(i);
String id = c.getString(TAG_ID);
String type = c.getString(TAG_TYPE);
String description = c.getString(TAG_DESCRIPTION);
String num = c.getString(TAG_NUM);
String num2 = c.getString(TAG_NUM2);
int imageint = getResources().getIdentifier(c.getString(TAG_TYPE) , "drawable", getPackageName());
String image = String.valueOf(imageint);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TYPE, type);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_NUM, num);
map.put(TAG_NUM2, num2);
map.put(TAG_IMAGE, image);
QueueList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
the code the set the DATA into the listview
ListAdapter adapter = new SimpleAdapter(
QueueActivity.this,
QueueList,
R.layout.queue_row,
new String[] { TAG_ID, TAG_DESCRIPTION, TAG_NUM, TAG_NUM2, TAG_IMAGE},
new int[] { R.id.queueid, R.id.description, R.id.num, R.id.num2, R.id.list_image });
setListAdapter(adapter);
now i want to set a setBackgroundResource to R.drawable.customlistviewback
if the db_id (TAG_ID) = int info
For ex, int info = 6;
To do row-level view modifications you will probably need to use a custom Adapter. Typically you override the getView function and make your changes there. See how to customize listview row android for a decent example.