Skip to content Skip to sidebar Skip to footer

Show Photo In Listview Using Path

I am trying to show all my pictures in a listview. I have an sqlite db where i save the image's path and now i wanted to know how can i show the image. I already know how to conver

Solution 1:

image View in an xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="30dp"android:src="@drawable/ic_launcher" /></RelativeLayout>

main activity xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:text="List of Images"android:textAppearance="?android:attr/textAppearanceMedium" /><ListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/textView1"android:layout_centerHorizontal="true" ></ListView></RelativeLayout>

Main Activity class: ImageListView, I am showing the images that are in the **res->drawable-mdpi folder

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

publicclassImageListViewextendsActivity {

    private ImageAdapter adapter;
    private ListView imageList;

    private ArrayList<Integer> imagePaths= newArrayList<Integer>(); // Edit your code here..@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
        setContentView(R.layout.image_listview);

        // Edit your code here..
        imagePaths.add(R.drawable.image1);
        imagePaths.add(R.drawable.image2);
        imagePaths.add(R.drawable.image3);

        imageList= (ListView) findViewById(R.id.listView1);
        adapter= newImageAdapter(getBaseContext(), imagePaths);
        imageList.setAdapter(adapter);      
    }
}

Custom Adapter: ImageAdapter

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

publicclassImageAdapterextendsBaseAdapter {

    staticclassRowItemHolder{
        ImageView imageView;
    }
    private Context context;
    private ArrayList<Integer> imagePaths= newArrayList<Integer>();

    publicImageAdapter(Context baseContext, ArrayList<Integer> imagePaths) {
    // TODO Auto-generated constructor stubthis.context= baseContext;
        this.imagePaths= imagePaths;
    }

    @OverridepublicintgetCount() {
    // TODO Auto-generated method stubreturn imagePaths.size();
    }

    @Overridepublic Object getItem(int position) {
    // TODO Auto-generated method stubreturnnull;
    }

    @OverridepubliclonggetItemId(int position) {
    // TODO Auto-generated method stubreturn0;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view;
    view= convertView;
    RowItemHolderholder=null;
    if(view== null){
            LayoutInflaterin=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = in.inflate(R.layout.image_view, parent, false);
            holder= newRowItemHolder();
            holder.imageView=(ImageView) view.findViewById(R.id.imageView1);
        view.setTag(holder);
    } else{
            holder = (RowItemHolder) view.getTag();
    }
    //Edit the code here according to you needs.. //like creating option and converting to Bitmap, //or you can do this job in the main activity.
    holder.imageView.setImageResource(imagePaths.get(position));
    return view;
}
}

Post a Comment for "Show Photo In Listview Using Path"