Android 显示和分析从DB到listview的图像
我必须在android应用程序的listview中显示mysql数据库中的数据。我已成功解析文本数据并将其显示在自定义listview中,但图像未被解析并显示在列表中。我尝试过使用imageloader、文件缓存、内存缓存,但仍然没有成功 如果任何人对遗漏的内容或应添加的内容有任何想法,将不胜感激Android 显示和分析从DB到listview的图像,android,mysql,android-listview,uri,adapter,Android,Mysql,Android Listview,Uri,Adapter,我必须在android应用程序的listview中显示mysql数据库中的数据。我已成功解析文本数据并将其显示在自定义listview中,但图像未被解析并显示在列表中。我尝试过使用imageloader、文件缓存、内存缓存,但仍然没有成功 如果任何人对遗漏的内容或应添加的内容有任何想法,将不胜感激 public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-gener
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tourname;
TextView duration;
ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_row1, parent, false);
// Get the position from the results
// Locate the TextViews in listview_item.xml
tourname = (TextView) itemView.findViewById(R.id.themeTourList_title);
duration = (TextView) itemView.findViewById(R.id.themeTourList_price);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.themeTourList_image);
// Capture position and set results to the TextViews
try
{
tourname.setText(mJSONArray.getJSONObject(position).getString("tour_name"));
duration.setText(""+mJSONArray.getJSONObject(position).getString("nights")+" Nights - "+mJSONArray.getJSONObject(position).getString("days")+" Days");
try{
URL url = new URL("www.futurolicht.com/"+mJSONArray.getJSONObject(position).getString("pic"));
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)(url).getContent());
flag.setImageBitmap(bitmap);
}
尝试使用以下方法下载图像:
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
我会写一个自定义游标或适配器
public class WhosNextAdapter extends CursorAdapter
{
public WhosNextAdapter(Context context, Cursor cursor, boolean autoRequery) {
super(context, cursor, autoRequery);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//This is were you would check your cursor for style (I think that is 1,2,3 your column)
int style = cursor.getInt(cursor.getColumnIndex("style"));
ImageView img = (ImageView) view.findViewById(R.id.yourImageViewId);
switch (style) {
case 1:
img.setImageResource(R.drawable.yourImageForStyle1);
break;
case 2:
img.setImageResource(R.drawable.yourImageForStyle2);
break;
//etc.
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Inflate layout R.layout.artist_list_item
//Call bindView passing inflated layout, context, and cursor
//return layout
}
}