JetPro
JetPro

Reputation: 1054

Inserting image from assets folder into a ListView

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    JSONObject json = jParser.getJSONFromUrl("http://domain.com/directory/database/retrieveComments.php?placeId=" + stringPlaceId);
    try
    {
        commentsRatingsArray = json.getJSONArray("commentsRatings");
        for(int i = 0; i < commentsRatingsArray.length(); i++)
        {
        JSONObject jsonObject = commentsRatingsArray.getJSONObject(i);
        String dbUserFullName = jsonObject.getString(TAG_FULLNAME);
        String dbUserEmail = jsonObject.getString(TAG_EMAIL);
        String dbComment = jsonObject.getString(TAG_COMMENT);
        String dbRating = jsonObject.getString(TAG_RATING);
        String dbDate = jsonObject.getString(TAG_DATE);
        String dbTime = jsonObject.getString(TAG_TIME);

        HashMap<String, String> map = new HashMap<String, String>();
        map.put(TAG_FULLNAME, dbUserFullName);
        map.put(TAG_EMAIL, dbUserEmail);
        map.put(TAG_COMMENT, dbComment);
        map.put(TAG_RATING, dbRating);
        map.put(TAG_DATE, dbDate);
        map.put(TAG_TIME, dbTime);

        list.add(map);
    }   
}
catch (Exception e)
{
     e.printStackTrace();
     Toast.makeText(getBaseContext(), "Connection to the server is lost. Please check your internet connection.", Toast.LENGTH_SHORT).show();
}

ListAdapter adapter = new SimpleAdapter
(DisplayCommentsRatings.this, list, R.layout.commentrating,

     new String[] { TAG_FULLNAME, TAG_EMAIL, TAG_COMMENT, TAG_DATE,  TAG_TIME },
     new int[] {R.id.tvUserFullName, R.id.tvUserEmail, R.id.tvUserComment, R.id.tvDate, R.id.tvTime });

     setListAdapter(adapter);

Here's my code, I'm getting these JSON Array values from my database. I just want to know how to change an image's src inside a list view. Because I will only use 5 images, I decided to include these images in my assets folder instead of uploading them to the web.

Can someone give me an idea to make this possible?

Here's my XML code:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvUserFullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="12dip"
        android:textStyle="bold"/>
            //This is the imageView where I will display the image from the assets folder
    <ImageView
        android:id="@+id/ivUserRating"
        android:layout_width="100dip"
        android:layout_height="fill_parent"
        android:src="@drawable/zerostar"/>

</LinearLayout>

<TextView
    android:id="@+id/tvUserEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[email protected]"
    android:textSize="9dip"/>

<TextView
    android:id="@+id/tvUserComment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text='"This is a comment. This is a comment. This is a comment. This is a comment. This is a comment."'
    android:textSize="10dip"
    android:layout_margin="3dip"
    android:textColor="#000000"
    android:maxLength="300"/>

<TextView
    android:id="@+id/tvDate"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="August 1, 2010"
    android:textColor="#000000"
    android:textSize="8dip"
    android:layout_gravity="right"/>

<TextView
    android:id="@+id/tvTime"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="08:20 PM"
    android:textColor="#000000"
    android:textSize="8dip"
    android:layout_gravity="right"/>

Upvotes: 0

Views: 2768

Answers (2)

malajisi
malajisi

Reputation: 2233

Use SimpleAdapter as normal, but be sure to override you "adapter"'s the setViewBinder method like:

adapter.setViewBinder(new ViewBinder() {   
    public boolean setViewValue(View view, Object data,   
    String textRepresentation) {   
    // Check wether it's ImageView and the data   
    if(view instanceof ImageView && data instanceof Bitmap){   
        ImageView iv = (ImageView) view;   
        iv.setImageBitmap((Bitmap) data);   
        return true;   
    }else   
        return false;   
    }   
});  

then use a getBitmap() to get the assert image

public Bitmap getBitmap( String path, int i ){   
        Bitmap mBitmap = null;   
        try {  
            AssetManager assetManager = getAssets(); 
            String[] files = null; 

            files = assetManager.list( "smartmodel/" + path ); 
            Log.i( "Assert List", files[1].toString() );
            // Pass ur file path, here is one in assert/smartmodel/ filer
            mBitmap = BitmapFactory.decodeStream( this.getAssets().open( "smartmodel/" + path + "/"+ files[i]) );   
        } catch (Exception e) {   
            e.printStackTrace();   
        }
        return mBitmap;   
    }

Last, in your Simple adapter List parameter, put

map.put( "ItemImage", getBitmap( gridItemName, i ));

Your passed getBitmap(...) will be show.

Upvotes: 1

Samir Mangroliya
Samir Mangroliya

Reputation: 40406

check position and use like,

Bitmap bmp=null;

if(position==0){

   bitmap=getBitmap("img0.png");

}else if  (position==1){

   bitmap=getBitmap("img1.png");
}
.
.
.

Method::

private Bitmap getBitmap(String name) throws IOException
        {
            AssetManager asset = getAssets();

            InputStream is = asset.open(name);
            Bitmap bitmap = BitmapFactory.decodeStream(is);

            return bitmap;
        }

Upvotes: 0

Related Questions