Display Image In List (Urgent Required)

All your problems with Audio, Video and Images.

Display Image In List (Urgent Required)

Postby ankita gupta » Mon Jun 08, 2009 11:59 am

I get bitmap image @ runtime, now have to display images in list using SimpleAdapter .
I am adding bitmap images in ArrayList, then pass this ArrayList in SimpleAdapter.
When I do this, I am getting error bad bitmap uri.
I have also tried ImageView with those bitmaps & put ImageViews in ArrayList of SimpleAdapter, then also getting error bad bitmap uri.
Could any one help me.
ankita gupta
Freshman
Freshman
 
Posts: 6
Joined: Thu May 14, 2009 6:50 am

Top

Postby darolla » Thu Mar 04, 2010 7:35 pm

same problem here. did you solve it?
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Postby Luki » Fri Mar 05, 2010 11:04 pm

If you like to give an image to a list with simple adapter, you need an Resource Id for the image (what means that you have the image prestored in you res-folder). In the documentation to the SimpleAdapter it is written that you can also give a String with the Uri to the Image, but I have never been able to make it work with that, only with resource id. Maybe you can do it with a Viewbinder implementing it yourself, but I don't know how to do this.

Cheers
Luki
Developer
Developer
 
Posts: 32
Joined: Fri Jul 24, 2009 12:59 pm

Postby darolla » Sun Mar 07, 2010 6:27 pm

Luki, you're genius !

:)

It worked the way you said.

First some infos:

http://developer.android.com/reference/ ... inder.html

"This class can be used by external clients of SimpleAdapter to bind values to views. You should use this class to bind values to views that are not directly supported by SimpleAdapter or to change the way binding occurs for views supported by SimpleAdapter."

http://developer.android.com/reference/ ... apter.html

"ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked."

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public class MyViewBinder implements ViewBinder {
  3.  
  4.  
  5.  
  6.                 @Override
  7.  
  8.                 public boolean setViewValue(View view, Object data,
  9.  
  10.                                 String textRepresentation) {
  11.  
  12.                        
  13.  
  14.                         if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
  15.  
  16.                                 ImageView iv = (ImageView) view;
  17.  
  18.                                 Bitmap bm = (Bitmap) data;     
  19.  
  20.                                 iv.setImageBitmap(bm); 
  21.  
  22.                                 return true;
  23.  
  24.                         }
  25.  
  26.  
  27.  
  28.                         return false;
  29.  
  30.                 }
  31.  
  32.         }
  33.  
  34.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 SimpleAdapter notes = new SimpleAdapter(this, list,
  3.  
  4.                                 R.layout.meinungen_list_row, PARAM, new int[] { R.id.icon,
  5.  
  6.                                                 R.id.name, R.id.content });
  7.  
  8.  
  9.  
  10.                 notes.setViewBinder(new MyViewBinder());
  11.  
  12.  
  13.  
  14.                 setListAdapter(notes);
  15.  
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Greetings,
Marco
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Postby Luki » Sun Mar 07, 2010 11:15 pm

Ou wow cool, you made it work! I also tried to implement the ViewBinder myself once. But did not succeed. Thanks for sharing the code.

Cheers, Luki
Luki
Developer
Developer
 
Posts: 32
Joined: Fri Jul 24, 2009 12:59 pm

Re: Display Image In List (Urgent Required)

Postby monta007 » Fri Jun 10, 2011 3:51 pm

cool ,Thanks for sharing the code.
but when i cliqued in item of listview to pass for second item (lv.setOnItemClickListener(new OnItemClickListener() doesn't worked), i will put my code and please help me.

Restaurant.java
package com.pxr.tutorial.json;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.pxr.tutorial.xmltest.R;

public class Restaurant extends ListActivity {
String urlresto= "http://www.monresto.net/partenaires/restaurant/";
int width = 100;
int height = 200;

//TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);;
setContentView(R.layout.listplaceholder);

Button button = new Button(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
button.setText("Item name = " + bundle.getString("id")
+ " --- Go Back ");
} else {
button.setText("Go Back");
}


ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();


JSONObject json = JSONfunctions.getJSONfromURL("http://monresto.net/android/marsa.php?id_cp="+ bundle.getString("id"));

try{

JSONArray monrestodb1 = json.getJSONArray("monrestodb1");

for(int i=0;i<monrestodb1.length();i++){
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject e = monrestodb1.getJSONObject(i);

map.put("name", String.valueOf(i));
map.put("name", "" + e.getString("nom"));
map.put("id", "" + e.getString("id"));

//map.put("photo",bitmap);
map.put("photo", "" + e.getString("photo"));
URL aURL = new URL("http://monresto.net/partenaires/restaurant/"+ e.getString("photo"));

Bitmap bitmap = BitmapFactory.decodeStream(aURL.openStream());

//bitmap.getScaledHeight(200);
//bitmap.getScaledWidth(100);
map.put("photo",bitmap);


//map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

SimpleAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listmontplaisir,
new String[] { "name", "photo","id" },
new int[] { R.id.montplaisirtext , });
adapter.setViewBinder(new MyViewBinder());
setListAdapter(adapter);

final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("log_tag", "Erreur"+lv);
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Restaurant.this, "NAME '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(Restaurant.this, Famille_plat.class);
//intent.putExtra("url", "http://monresto.net/android/marsa.php");
String txt = o.get("id");
intent.putExtra("id", txt);

startActivity(intent);
}


});

}

}

MyViewBinder.java

package com.pxr.tutorial.json;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter.ViewBinder;

public class MyViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Object data,String textRepresentation) {
if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
ImageView iv = (ImageView) view;
Bitmap bm = (Bitmap) data;
iv.setImageBitmap(bm);
return true;
}

return false;
}
}


and my XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

>

<ImageView
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10px"
/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10px"
android:layout_weight="1"
>


<TextView android:id="@+id/montplaisirtext"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="16px"
android:textStyle="bold"
/>
<RatingBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:id="@+id/star"
android:numStars="5"
android:stepSize="0.1"
android:isIndicator="true"
/>


</LinearLayout>
</LinearLayout>

please help me :(
monta007
Freshman
Freshman
 
Posts: 2
Joined: Sat Apr 23, 2011 1:03 pm

Top

Re: Display Image In List (Urgent Required)

Postby m1shk4 » Mon Jun 13, 2011 9:57 am

onItemClickListener is not working, because child views are focusable
set focusable and focusableInTouchMode to false and it will work
m1shk4
Developer
Developer
 
Posts: 38
Joined: Wed Jan 12, 2011 4:26 pm
Location: UA

Re: Display Image In List (Urgent Required)

Postby whytopersist » Mon Sep 05, 2011 10:55 am

It's my first time to come here.Haha,Nice to meet everybody.I learn from my friends that this forum is very interesting.

Coach outlet | Coach Handbags | Coach Bags
whytopersist
Freshman
Freshman
 
Posts: 6
Joined: Mon Sep 05, 2011 10:37 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 29 guests