android - 将图像从数据库显示并解析到 ListView

标签 android mysql android-listview uri adapter

我必须在我的 Android 应用程序的 ListView 中显示来自 mysql 数据库的数据。我已成功解析文本数据并将其显示在自定义 ListView 中,但图像未被解析并显示在列表中。我尝试过使用imageloader、文件缓存、内存缓存,但仍然没有成功。

如果有人知道缺少什么或应该添加什么,任何帮助将不胜感激。

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);

关于android - 将图像从数据库显示并解析到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18400036/

相关文章:

android - Android 中的 ListView 是如何工作的?我很困惑

android - ListView invalidate() 与 invalidateViews() 之间的区别?

mysql - 如何更改mysql 中新创建的VIEW 中的属性?

mysql - SQL内连接同一个表以获得最大值

android - 为什么 nohup 仍然导致在 shell 脚本中运行的进程终止?

Android 读取浏览器历史记录

php - 在MySQL中创建一个 View 表,但从2个表中读取数据

android - 数据丢失并在横向和纵向模式android中强制关闭

android - 操作栏 fragment Activity

android - 即使应用程序关闭也能使用警报管理器?