如何在图像视图中从图像网址获取图像
我的imageUrl来自databaseadapter
在Fields类中,LocationImage dataype是字符串,但在 setBackgroundResource 方法中,它要求int值作为参数。 LocationImage url是从数据库获取的,所以我把它作为字符串变量。
代码行在这里。
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");
}
}
}
答案 0 :(得分:2)
如果你有图像网址,请使用以下代码将图像从url设置为imageview
Bitmap mbmp = BitmapFactory.decodeStream(new java.net.URL("urlname").openStream());
Imageview_ref.setImageBitmap(mbmp);