2015-04-06 197 views
-1

後,我決定實施Universal Image Loader,因爲我已經實施了轉換URLDrawable的方法,但因爲我不知道有多少圖像將返回我SQLite查詢我決定實施Image Loader。 .. 事情是我卡在這一刻,因爲我以爲我做了什麼GitHub說,但在我加載Image它保持白色,從不加載。實施畢加索不加載圖像

在我Adapter class我已經改變了drawable的路線爲:

Picasso.with(context) 
      .load(Uri.parse(String.valueOf(item.icon))) 
      .resize(180, 180) 
      .placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon); 

它的工作原理,beucase它顯示了喲我ic_launcher圖標...但永遠不會改變的真實圖像。

在我的課,我取(在我的OnCreate()),我有這樣的數據:

new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         Thread.sleep(2000); 


        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 

        new MyAsyncTask().execute(); 

        getActivity().runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
        // progress.dismiss(); 
         } 
        }); 
       } 

      }).start(); 
     } 

然後,我創建了一個內部類,我把數據放到我的ListView ...但它不沒有用。我不知道如果我必須刪除這些方法,因爲我已將其更改爲Picasso

private class MyAsyncTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     Conexion = new MarketSQLite(getActivity(), "market", null, 1); 
     mItems = new ArrayList<ListViewItem>(); 

     db = Conexion.getReadableDatabase(); 

     Cursor c; 

     c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null); 
     c.moveToFirst(); 
     if (c != null) { 
      do { 
       for (int i = 0; i < c.getColumnCount(); i++) { 
        Title = c.getString((c.getColumnIndex("NOM_OFER"))); 
        Preu = c.getColumnIndex("PREU_OFERTA"); 
        percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE"))); 
        data_f = c.getString((c.getColumnIndex("DATA_F"))); 
        URLTest = c.getString((c.getColumnIndex("FOTO"))); 
        FOTO = Imagehandler(URLTest); 
        Log.e("", "" + c.getString(i)); 

        // initialize and set the list adapter 


        // Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show(); 
       } 
       mItems.add(new ListViewItem(FOTO, Title, Preu.toString(), percent, data_f)); 


      }while (c.moveToNext()); 
     } 
     c.close(); 

return null; 
    } 


    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     myAdapter = new ListViewDemoAdapter(getActivity(), mItems); 
     setListAdapter(myAdapter); 
    } 
} 

ImageHandler是一種方法,我已經創建之前是這樣的:

protected Drawable Imagehandler(String url) { 
     try { 
      url=url.replaceAll(" ", "%20"); 
      InputStream is = (InputStream)this.fetch(url); 
      Drawable d = Drawable.createFromStream(is, "src"); 
      return d; 
     } catch (MalformedURLException e) 
     { 
      System.out.println(url); 
      System.out.println("error at URI"+e); 
      return null; 
     } 
     catch (IOException e) 
     { 
      System.out.println("io exception: "+e); 
      System.out.println("Image NOT FOUND"); 
      return null; 
     } 
    } 

    protected Object fetch(String address) throws MalformedURLException,IOException { 
     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    } 

我不知道爲什麼,是不是我的ListView的圖像加載,如果它顯示了所有的休息數據...

回答

2

而是可繪製的,試圖讓URL字符串在適配器像

改變從

public ListViewItem(Drawable icon, String title, String precio, String descuento, String date) { 
    this.icon = icon; 
    this.title = title; 
    this.precio = precio; 
    this.descuento = descuento; 
    this.date = date; 
} 

public ListViewItem(String icon_url, String title, String precio, String descuento, String date) { 
     this.icon_url = icon_url; 
     this.title = title; 
     this.precio = precio; 
     this.descuento = descuento; 
     this.date = date; 
    } 

,並使用畢加索,你加載你的ImageView這樣的 -

Picasso.with(context) 
      .load(icon_url)) 
      .resize(180, 180) 
      .placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon); 

1)你的ListViewItem類應該是這樣的 -

public class ListViewItem { 
    public final String icon;  // the drawable for the ListView item ImageView 
    public final String title;  // the text for the ListView item title 
    public final String precio;  // the price for the ListView item 
    public final String descuento; // the price for the discount for the ListView item 
    public final String date;  //the date for the sale for the ListView item 

    // the text for the ListView item description 

    public ListViewItem(String icon_url, String title, String precio, String descuento, String date) { 
     this.icon = icon_url; 
     this.title = title; 
     this.precio = precio; 
     this.descuento = descuento; 
     this.date = date; 
    } 
} 

2)ListViewDemoAdapterClass

public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> { 
Context context; 
    public ListViewDemoAdapter(Context context, List<ListViewItem> items) { 
     super(context, R.layout.listview_item, items); 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder viewHolder; 

     if(convertView == null) { 
      // inflate the GridView item layout 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.listview_item, parent, false); 

      // initialize the view holder 
      viewHolder = new ViewHolder(); 
      viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon); 
      viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle); 
      viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice); 
      viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount); 
      viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tvDatas); 
      convertView.setTag(viewHolder); 
     } else { 
      // recycle the already inflated view 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     // update the item view 
     ListViewItem item = getItem(position); 
     Picasso.with(context) 
       .load(item.icon) 
       .resize(180, 180) 
       .placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon); 
     viewHolder.tvTitle.setText(item.title); 
     viewHolder.tvDiscount.setText(item.descuento); 
     viewHolder.tvPrice.setText(item.precio); 
     viewHolder.tvDate.setText(item.date); 

     return convertView; 
    } 


    private static class ViewHolder { 
     ImageView ivIcon; 
     TextView tvTitle; 
     TextView tvDiscount; 
     TextView tvPrice; 
     TextView tvDate; 
    } 
} 
  • ListFragment代碼,只需添加此

    Cursor c; 
    
    c = db.rawQuery("Select 
    NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null); 
    c.moveToFirst(); 
    if (c != null) { 
    do { 
    for (int i = 0; i < c.getColumnCount(); i++) { 
    Title = c.getString((c.getColumnIndex("NOM_OFER"))); 
    Preu = c.getColumnIndex("PREU_OFERTA"); 
    percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE"))); 
    data_f = c.getString((c.getColumnIndex("DATA_F"))); 
    URLTest = c.getString((c.getColumnIndex("FOTO"))); 
    
  • 希望這會有所幫助:)

    +0

    像魅力一樣工作,謝謝;) – 2015-04-06 14:31:23

    2

    你只需要添加你的畢加索代碼段爲您ImageHandler方法下面並沒有什麼else-

    Picasso.with(context) 
          .load(url)) 
          .resize(180, 180) 
          .placeholder(R.drawable.ic_launcher).into(your_imageview); 
    

    您不需要下載圖像或製作位圖或將其轉換爲可繪製的圖形以便從url加載。希望這可以幫助你。

    +0

    我的網址不在同一個類中,我的意思是我在查詢我的SQLite – 2015-04-06 11:53:36

    +1

    時得到的網址,這應該不是問題。你從任何地方檢索網址,然後加載網址。只有一件事是明確的URI和URL不是一回事。 – 2015-04-06 12:18:21

    +0

    但是我必須定義網址......並且在我的其他班級 – 2015-04-06 12:32:19