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