使用毕加索库的ListView [英] Using Picasso library with ListView
问题描述
我想要做的就是适应我的自定义的ListView适配器由毕加索库使用从网络上获取的图像。我相信我有,以接受来自毕加索的图像我的适配器变了,但我不能确定如何改变我的实现使用一个ListView接受它。我相信我有访问holder.imageIcon,但我不知道如何得到它运行起来。我的code是如下:
History.java
公共类历史{
公共字符串分;
公共字符串游戏类型;
公共毕加索图标;
公共历史(){
超();
}
公共历史(字符串得分,串游戏类型,毕加索图标){
超();
this.score =得分;
this.gametype =游戏类型;
this.icon =图标;
}
}
HistoryAdapter.java
公共类HistoryAdapter扩展ArrayAdapter<历史> {
上下文语境;
INT layoutResId;
历史数据[] = NULL;
公共HistoryAdapter(上下文的背景下,INT layoutResId,历史[]数据){
超级(上下文,layoutResId,数据);
this.layoutResId = layoutResId;
this.context =背景;
this.data =数据;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
HistoryHolder支架=无效;
如果(convertView == NULL)
{
LayoutInflater充气=((活动)上下文).getLayoutInflater();
convertView = inflater.inflate(layoutResId,父母,假);
持有人=新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(保持器);
}
其他
{
支架=(HistoryHolder)convertView.getTag();
}
历史记录=数据[位置]
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
返回convertView;
}
静态类HistoryHolder
{
ImageView的ImageIcon的;
TextView的textTitle;
TextView的textScore;
}
}
执行
历史[] historyData =新的历史[games.length()];
的for(int i = 0; I< games.length();我++){
JSONObject的C = games.getJSONObject(我);
JSONObject的gameStats = games.getJSONObject(我).getJSONObject(TAG_STATS);
键入[I] = c.getString(TAG_TYPE);
champId [I] = c.getString(championId);
CS [i] = gameStats.getString(minionsKilled);
杀死[I] = gameStats.getString(championsKilled);
死亡[I] = gameStats.getString(numDeaths);
助攻[I] = gameStats.getString(助攻);
赢得[I] = gameStats.getString(赢);
如果(夺冠[我] .equals(真))
赢得[I] =胜利;
其他
赢得[I] =失败;
如果(类[I] .equals(RANKED_SOLO_5x5))
键入[I] =排名(独奏);
如果(类[I] .equals(CAP_5x5))
键入[I] =TeamBuilder;
如果(类[I] .equals(正常))
键入[I] =未排序;
得分[I] =杀死[I] +/+死亡[I] +/+协助[I];
historyData [我] =新的历史(比分[我],champId [I],R.drawable.ic_launcher); //占位符图像
}
如果(historyData == NULL){
historyData [0] =新的历史(没有游戏找到,N / A,R.drawable.ic_launcher); //使用毕加索占位符
Log.i(数据,+ historyData);
}
适配器=新HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(适配器);
list_item.xml
< RelativeLayout的
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =机器人:ATTR /列表preferredItemHeight
机器人:后台=#111111
机器人:填充=6dip>
< ImageView的
机器人:ID =@ + ID /图标
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentTop =真
机器人:layout_marginRight =6dip
机器人:contentDescription =TODO
机器人:SRC =@可绘制/ ic_launcher/>
<的TextView
机器人:ID =@ + ID /分
机器人:文字颜色=#C49246
机器人:layout_width =FILL_PARENT
机器人:layout_height =26dip
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:layout_marginLeft =5DP
机器人:layout_toRightOf =@ ID /图标
机器人:ellipsize =金字招牌
机器人:单线=真
机器人:文本=0/0/0 KDA
机器人:TEXTSIZE =12SP/>
<的TextView
机器人:ID =@ + ID /游戏类型
机器人:文字颜色=#C49246
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_above =@ ID /分
机器人:layout_alignParentRight =真
机器人:layout_alignParentTop =真
机器人:layout_alignWithParentIfMissing =真
机器人:layout_marginLeft =5DP
机器人:layout_toRightOf =@ ID /图标
机器人:重力=center_vertical
机器人:TEXTSIZE =16SP/>
< / RelativeLayout的>
有两件事情你需要改变:
1) History.icon
应该是字符串
URL的图标,而不是一个毕加索
对象。您也可以使用文件
,乌里
或 INT
,但字符串
URL可能是你想要的东西。
2)修改你的适配器的
getView()
方法使用加载图标毕加索
(参见前 getView最后一行()
返回 convertView
):
公共类HistoryAdapter扩展ArrayAdapter<历史> {
上下文语境;
INT layoutResId;
历史数据[] = NULL;
公共HistoryAdapter(上下文的背景下,INT layoutResId,历史[]数据){
超级(上下文,layoutResId,数据);
this.layoutResId = layoutResId;
this.context =背景;
this.data =数据;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
HistoryHolder支架=无效;
如果(convertView == NULL)
{
LayoutInflater充气=((活动)上下文).getLayoutInflater();
convertView = inflater.inflate(layoutResId,父母,假);
持有人=新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(保持器);
}
其他
{
支架=(HistoryHolder)convertView.getTag();
}
历史记录=数据[位置]
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
Picasso.with(this.context).load(history.icon)。走进(holder.imageIcon)
返回convertView;
}
静态类HistoryHolder
{
ImageView的ImageIcon的;
TextView的textTitle;
TextView的textScore;
}
}
What I'm trying to do is adapt my custom ListView adapter to use images fetched from the web by the Picasso library. I believe I have my adapter changed in order to accept an image from Picasso, but I am unsure how to change my implementation to accept it using a ListView. I believe I have to access holder.imageIcon, but I am not sure how to get it up and running. My code is as follows.
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;
}
}
Implementation
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>
There are 2 things you need to change:
1) History.icon
should be the String
url of the icon, not a Picasso
object. You can also use a File
, Uri
, or int
, but a String
url is probably what you want.
2) Modify your Adapter's
getView()
method to load the icon using Picasso
(see the last line before getView()
returns the convertView
):
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);
Picasso.with(this.context).load(history.icon).into(holder.imageIcon)
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}
这篇关于使用毕加索库的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!