android – 使用Picasso库和ListView

android – 使用Picasso库和ListView,第1张

概述我正在尝试做的是调整我的自定义ListView适配器以使用Picasso库从网络获取的图像.我相信我已经更改了适配器以接受来自毕加索的图像,但我不确定如何使用ListView更改我的实现以接受它.我相信我必须访问holder.imageIcon,但我不知道如何让它运行起来.我的代码如下. History.java public class History { public String s 我正在尝试做的是调整我的自定义ListVIEw适配器以使用Picasso库从网络获取的图像.我相信我已经更改了适配器以接受来自毕加索的图像,但我不确定如何使用ListVIEw更改我的实现以接受它.我相信我必须访问holder.imageIcon,但我不知道如何让它运行起来.我的代码如下.

History.java

public class History {    public String score;    public String gametype;    public Picasso icon;    public History() {        super();    }    public History(String score,String gametype,Picasso icon) {        super();        this.score = score;        this.gametype = gametype;        this.icon = icon;    }}

HistoryAdapter.java

public class HistoryAdapter extends ArrayAdapter<History> {    Context context;    int layoutResID;    History data[] = null;    public HistoryAdapter(Context context,int layoutResID,History[] data) {        super(context,layoutResID,data);        this.layoutResID = layoutResID;        this.context = context;        this.data = data;    }    @OverrIDe    public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {        HistoryHolder holder = null;        if(convertVIEw == null)        {            LayoutInflater inflater = ((Activity)context).getLayoutInflater();            convertVIEw = inflater.inflate(layoutResID,parent,false);            holder = new HistoryHolder();            holder.imageIcon = (ImageVIEw)convertVIEw.findVIEwByID(R.ID.icon);            holder.textTitle = (TextVIEw)convertVIEw.findVIEwByID(R.ID.gameType);            holder.textscore = (TextVIEw)convertVIEw.findVIEwByID(R.ID.score);            convertVIEw.setTag(holder);        }        else        {            holder = (HistoryHolder)convertVIEw.getTag();        }        History history = data[position];        holder.textscore.setText(history.score);        holder.textTitle.setText(history.gametype);        holder.imageIcon.setimageResource(history.icon);        return convertVIEw;    }    static class HistoryHolder    {        ImageVIEw imageIcon;        TextVIEw textTitle;        TextVIEw textscore;    }}

履行

History[] historyData = new History[games.length()];for(int i = 0; i < games.length(); i++) {                    JsONObject c = games.getJsONObject(i);                    JsONObject gameStats = games.getJsONObject(i).getJsONObject(TAG_STATS);                    type[i] = c.getString(TAG_TYPE);                    champID[i] = c.getString("championID");                    cs[i] = gameStats.getString("minionsKilled");                    kills[i] = gameStats.getString("championsKilled");                    deaths[i] = gameStats.getString("numDeaths");                    assists[i] = gameStats.getString("assists");                    win[i] = gameStats.getString("win");                    if(win[i].equals("true"))                        win[i] = "Victory";                    else                        win[i] = "Defeat";                    if(type[i].equals("RANKED_SolO_5x5"))                        type[i] = "Ranked (Solo)";                    if(type[i].equals("CAP_5x5"))                        type[i] = "TeamBuilder";                    if(type[i].equals("norMAL"))                        type[i] = "Unranked";                    score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];                    historyData[i] = new History(score[i],champID[i],R.drawable.ic_launcher); // Placeholder image                }                if(historyData == null) {                    historyData[0] = new History("No game found","N/A",R.drawable.ic_launcher); // Use Picasso placeholder                    Log.i("Data","" + historyData);                }                adapter = new HistoryAdapter(MatchHistoryActivity.this,R.layout.List_adapter,historyData);                List.setAdapter(adapter);

List_item.xml

<relativeLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="?androID:attr/ListPreferredItemHeight"    androID:background="#111111"    androID:padding="6dip" >    <ImageVIEw        androID:ID="@+ID/icon"        androID:layout_wIDth="wrap_content"        androID:layout_height="fill_parent"        androID:layout_alignParentBottom="true"        androID:layout_alignParenttop="true"        androID:layout_marginRight="6dip"        androID:contentDescription="Todo"        androID:src="@drawable/ic_launcher" />    <TextVIEw        androID:ID="@+ID/score"        androID:textcolor="#C49246"        androID:layout_wIDth="fill_parent"        androID:layout_height="26dip"        androID:layout_alignParentBottom="true"        androID:layout_alignParentRight="true"        androID:layout_marginleft="5dp"        androID:layout_toRightOf="@ID/icon"        androID:ellipsize="marquee"        androID:singleline="true"        androID:text="0/0/0 KDA"        androID:textSize="12sp" />    <TextVIEw        androID:ID="@+ID/gameType"        androID:textcolor="#C49246"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_above="@ID/score"        androID:layout_alignParentRight="true"        androID:layout_alignParenttop="true"        androID:layout_alignWithParentIfMissing="true"        androID:layout_marginleft="5dp"        androID:layout_toRightOf="@ID/icon"        androID:gravity="center_vertical"        androID:textSize="16sp" /></relativeLayout>
@H_403_19@解决方法 您需要更改两件事:
1)History.icon应该是图标的String url,而不是Picasso对象.您也可以使用file,Uri或int,但String url可能就是您想要的.

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

public class HistoryAdapter extends ArrayAdapter<History> {    Context context;    int layoutResID;    History data[] = null;    public HistoryAdapter(Context context,false);            holder = new HistoryHolder();            holder.imageIcon = (ImageVIEw)convertVIEw.findVIEwByID(R.ID.icon);            holder.textTitle = (TextVIEw)convertVIEw.findVIEwByID(R.ID.gameType);            holder.textscore = (TextVIEw)convertVIEw.findVIEwByID(R.ID.score);            convertVIEw.setTag(holder);        }        else        {            holder = (HistoryHolder)convertVIEw.getTag();        }        History history = data[position];        holder.textscore.setText(history.score);        holder.textTitle.setText(history.gametype);        Picasso.with(this.context).load(history.icon).into(holder.imageIcon)        return convertVIEw;    }    static class HistoryHolder    {        ImageVIEw imageIcon;        TextVIEw textTitle;        TextVIEw textscore;    }}
总结

以上是内存溢出为你收集整理的android – 使用Picasso库和ListView全部内容,希望文章能够帮你解决android – 使用Picasso库和ListView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134772.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存