android – 使用Picasso库和ListView

前端之家收集整理的这篇文章主要介绍了android – 使用Picasso库和ListView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试做的是调整我的自定义ListView适配器以使用Picasso库从网络获取的图像.我相信我已经更改了适配器以接受来自毕加索的图像,但我不确定如何使用ListView更改我的实现以接受它.我相信我必须访问holder.imageIcon,但我不知道如何让它运行起来.我的代码如下.

History.java

  1. public class History {
  2. public String score;
  3. public String gametype;
  4. public Picasso icon;
  5.  
  6. public History() {
  7. super();
  8. }
  9.  
  10. public History(String score,String gametype,Picasso icon) {
  11. super();
  12. this.score = score;
  13. this.gametype = gametype;
  14. this.icon = icon;
  15. }
  16. }

HistoryAdapter.java

  1. public class HistoryAdapter extends ArrayAdapter<History> {
  2.  
  3. Context context;
  4. int layoutResId;
  5. History data[] = null;
  6.  
  7. public HistoryAdapter(Context context,int layoutResId,History[] data) {
  8. super(context,layoutResId,data);
  9. this.layoutResId = layoutResId;
  10. this.context = context;
  11. this.data = data;
  12. }
  13.  
  14. @Override
  15. public View getView(int position,View convertView,ViewGroup parent) {
  16. HistoryHolder holder = null;
  17.  
  18. if(convertView == null)
  19. {
  20. LayoutInflater inflater = ((Activity)context).getLayoutInflater();
  21. convertView = inflater.inflate(layoutResId,parent,false);
  22.  
  23. holder = new HistoryHolder();
  24. holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
  25. holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
  26. holder.textscore = (TextView)convertView.findViewById(R.id.score);
  27.  
  28. convertView.setTag(holder);
  29. }
  30. else
  31. {
  32. holder = (HistoryHolder)convertView.getTag();
  33. }
  34.  
  35. History history = data[position];
  36. holder.textscore.setText(history.score);
  37. holder.textTitle.setText(history.gametype);
  38. holder.imageIcon.setImageResource(history.icon);
  39.  
  40.  
  41. return convertView;
  42. }
  43.  
  44. static class HistoryHolder
  45. {
  46. ImageView imageIcon;
  47. TextView textTitle;
  48. TextView textscore;
  49. }
  50. }

履行

  1. History[] historyData = new History[games.length()];
  2.  
  3.  
  4. for(int i = 0; i < games.length(); i++) {
  5. JSONObject c = games.getJSONObject(i);
  6. JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS);
  7. type[i] = c.getString(TAG_TYPE);
  8. champId[i] = c.getString("championId");
  9. cs[i] = gameStats.getString("minionsKilled");
  10. kills[i] = gameStats.getString("championsKilled");
  11. deaths[i] = gameStats.getString("numDeaths");
  12. assists[i] = gameStats.getString("assists");
  13. win[i] = gameStats.getString("win");
  14.  
  15. if(win[i].equals("true"))
  16. win[i] = "Victory";
  17. else
  18. win[i] = "Defeat";
  19.  
  20. if(type[i].equals("RANKED_SOLO_5x5"))
  21. type[i] = "Ranked (Solo)";
  22.  
  23. if(type[i].equals("CAP_5x5"))
  24. type[i] = "TeamBuilder";
  25.  
  26. if(type[i].equals("NORMAL"))
  27. type[i] = "Unranked";
  28.  
  29. score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];
  30.  
  31. historyData[i] = new History(score[i],champId[i],R.drawable.ic_launcher); // Placeholder image
  32.  
  33. }
  34.  
  35. if(historyData == null) {
  36. historyData[0] = new History("No game found","N/A",R.drawable.ic_launcher); // Use Picasso placeholder
  37. Log.i("Data","" + historyData);
  38. }
  39.  
  40. adapter = new HistoryAdapter(MatchHistoryActivity.this,R.layout.list_adapter,historyData);
  41.  
  42. list.setAdapter(adapter);

list_item.xml

  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="?android:attr/listPreferredItemHeight"
  5. android:background="#111111"
  6. android:padding="6dip" >
  7.  
  8. <ImageView
  9. android:id="@+id/icon"
  10. android:layout_width="wrap_content"
  11. android:layout_height="fill_parent"
  12. android:layout_alignParentBottom="true"
  13. android:layout_alignParentTop="true"
  14. android:layout_marginRight="6dip"
  15. android:contentDescription="TODO"
  16. android:src="@drawable/ic_launcher" />
  17.  
  18. <TextView
  19. android:id="@+id/score"
  20. android:textColor="#C49246"
  21. android:layout_width="fill_parent"
  22. android:layout_height="26dip"
  23. android:layout_alignParentBottom="true"
  24. android:layout_alignParentRight="true"
  25. android:layout_marginLeft="5dp"
  26. android:layout_toRightOf="@id/icon"
  27. android:ellipsize="marquee"
  28. android:singleLine="true"
  29. android:text="0/0/0 KDA"
  30. android:textSize="12sp" />
  31.  
  32. <TextView
  33. android:id="@+id/gameType"
  34. android:textColor="#C49246"
  35. android:layout_width="fill_parent"
  36. android:layout_height="wrap_content"
  37. android:layout_above="@id/score"
  38. android:layout_alignParentRight="true"
  39. android:layout_alignParentTop="true"
  40. android:layout_alignWithParentIfMissing="true"
  41. android:layout_marginLeft="5dp"
  42. android:layout_toRightOf="@id/icon"
  43. android:gravity="center_vertical"
  44. android:textSize="16sp" />
  45.  
  46. </RelativeLayout>
@H_301_19@解决方法
您需要更改两件事:
1)History.icon应该是图标的String url,而不是Picasso对象.您也可以使用File,Uri或int,但String url可能就是您想要的.

2)修改Adapter的getView()方法,使用Picasso加载图标(请参阅getView()返回convertView之前的最后一行):

  1. public class HistoryAdapter extends ArrayAdapter<History> {
  2.  
  3. Context context;
  4. int layoutResId;
  5. History data[] = null;
  6.  
  7. public HistoryAdapter(Context context,false);
  8.  
  9. holder = new HistoryHolder();
  10. holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
  11. holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
  12. holder.textscore = (TextView)convertView.findViewById(R.id.score);
  13.  
  14. convertView.setTag(holder);
  15. }
  16. else
  17. {
  18. holder = (HistoryHolder)convertView.getTag();
  19. }
  20.  
  21. History history = data[position];
  22. holder.textscore.setText(history.score);
  23. holder.textTitle.setText(history.gametype);
  24. Picasso.with(this.context).load(history.icon).into(holder.imageIcon)
  25.  
  26.  
  27. return convertView;
  28. }
  29.  
  30. static class HistoryHolder
  31. {
  32. ImageView imageIcon;
  33. TextView textTitle;
  34. TextView textscore;
  35. }
  36. }

猜你在找的Android相关文章