开发者

How do i get image from imageUrl in imageView

开发者 https://www.devze.com 2023-04-05 07:39 出处:网络
How can i get image from image url in image view. My imageUrl is coming from databaseadapter. In Fields class LocationImage dataype is string but in setBackgroundResource method it is asking for int

How can i get image from image url in image view.

My imageUrl is coming from databaseadapter.

In Fields class LocationImage dataype is string but in setBackgroundResource method it is asking for int value as parameter. LocationImage url is getting from database, so that i've taken that as string variable.

code lines are here.

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FindPlaces extends ListActivity{

    private SQLiteDatabase DbLoc;
    ListView lv;
    int val;
    private ArrayList<Fields> results = new ArrayList<Fields>();
    @Override
    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.places);
        getallLocs();
        setListAdapter(new StudentListAdapter(this,  val, results));
    }

    class StudentListAdapter extends ArrayAdapter<Fields>{
        private ArrayList<Fields> locationDetails;
        private Context mContext;

        public StudentListAdapter(Context context,int textViewResourceId, ArrayList<Fields> results) {
            super(context, textViewResourceId, results);
            // TODO Auto-generated constructor stub
            System.out.println("Constructor StudentList Adapter...");
            this.locationDetails = results;
            mContext = context;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return results.size();
        }
        @Override
        public Fields getItem(int position) {
            // TODO Auto-generated method stub
            return locationDetails.get(position);
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return super.getItemId(position);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = convertView;
            if(v == null){
                LayoutInflater vl = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vl.inflate(R.layout.placeslist, null);
            }
            Fields o = results.get(position);

            if (o != null) {
                TextView iv = (TextView)v.findViewById(R.id.toptext);
                TextView tv_sNo = (TextView)v.findViewById(R.id.toptext1);
                ImageView tv_Image = (ImageView)v.findViewById(R.id.Locimage);

                iv.setText(o.getLocationName());                            
                //tv_sNo.setText("Status: "+ o.getOrderStatus());
                tv_sNo.setText(o.getLocationImage());   
                tv_Image.setBackgroundResource(o.getLocationImage());
            }
            DbLoc.close();
                            return开发者_如何转开发 v;
        }       
    }
    static class ViewHolder
    {
        TextView Locationname;
        ImageView Locationimage;
    }
    private void getallLocs() {
        // TODO Auto-generated method stub
        try {
            DatabaseHelper dbHelper = new DatabaseHelper(
                    this.getApplicationContext());
            DbLoc = dbHelper.getWritableDatabase();
            Cursor c = DbLoc.rawQuery("SELECT " + DatabaseHelper.LocationName+ " , " + DatabaseHelper.LocationImage + " FROM "
                    + DatabaseHelper.LOCATIONTABLE , null);
            System.out.println("SELECT " + DatabaseHelper.LocationLang+" , "+DatabaseHelper.LocationLat+" , "+ DatabaseHelper.LocationName
                    + " ," + DatabaseHelper.LocationImage + " FROM "
                    + DatabaseHelper.LOCATIONTABLE );
            if (c != null) {
                if (c.moveToFirst()) {
                    do {
                        String LocationName= c.getString(c.getColumnIndex("LocationName"));
                        String Mobile = c.getString(c
                                .getColumnIndex("LocationImage"));
                        Fields p = new Fields(LocationName, Mobile);
                        results.add(p);

                    } while (c.moveToNext());
                }
            }
        } catch (SQLiteException se) {
            Log.e(getClass().getSimpleName(),
            "Could not create or Open the database");
        } 
                }
}


if u got image url use the below code to set the image from url to imageview

Bitmap mbmp = BitmapFactory.decodeStream(new java.net.URL("urlname").openStream());
Imageview_ref.setImageBitmap(mbmp);
0

精彩评论

暂无评论...
验证码 换一张
取 消