1
我一直有一个问题,有时当一个新项目被添加到列表视图或用户滚动浏览列表视图时,图像变成了不同的东西。 这是我的适配器,我该如何解决这个问题?在ListView中更改图像? (解析和毕加索)
public class MyBaseAdapter extends BaseAdapter {
ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
String url;
ImageView iv;
ParseFile file;
public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context); // only context can also be used
}
@Override
public int getViewTypeCount() {
if (getCount() != 0)
return getCount();
return 1;
}
@Override
public int getCount() {
return myList.size();
}
@Override
public ListData getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
View v = convertView;
if(convertView == null) {
convertView = inflater.inflate(R.layout.list_view_item, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.tvTitle = detail(convertView, R.id.userNameBody, myList.get(position).getTitle());
mViewHolder.tvDesc = detail(convertView, R.id.destinyMessageBody, myList.get(position).getDescription());
mViewHolder.image = detail(convertView, R.id.profile_picture, myList.get(position).getImage());
Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");
//Scheme for Linkify, when a word matched tagMatcher pattern,
//that word is appended to this URL and used as content URI
String newActivityURL = "content://com.rhynoapplications.askmethis.hashtaglookup/";
//Attach Linkify to TextView
Linkify.addLinks(mViewHolder.tvDesc, tagMatcher, newActivityURL);
return convertView;
}
// or you can try better way
private TextView detail(View v, int resId, String text) {
TextView tv = (TextView) v.findViewById(resId);
tv.setText(text);
TextView tv2 = (TextView) v.findViewById(R.id.userNameBody);
url = tv2.getText().toString();
return tv;
}
private ImageView detail(View v, int resId, int icon) {
iv = (ImageView) v.findViewById(resId);
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("username", url);
userQuery.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> results, ParseException e) {
// results has the list of users with a hometown team with a winning record
for(ParseObject obj: results){
file = obj.getParseFile("profilePicture");
}
Picasso.with(context).load(file.getUrl()).resize(200,200).placeholder(R.drawable.destiny_image).into(iv);
}
});
return iv;
}
public class MyViewHolder {
public TextView tvTitle, tvDesc;
public ImageView image;
}
}
它的工作,但现在它的一定量的物品后复制的项目。 – Rhynoboy2009 2014-09-26 05:14:21
你是什么意思复制?你能解释一下实际发生的事情吗? – AndyN 2014-09-26 05:16:42
我有一个按钮,其中添加项目到列表视图,我会添加让我们说3个项目,当我再次按下按钮时,它会开始循环这3个,从顶部添加这些3项到列表视图反复而不是添加我所说的添加内容。 – Rhynoboy2009 2014-09-26 05:25:01