我已经搜索过这个bug并且还进行了优化。我还使用了Viewholder类和类似的方法,但仍然无法解决它。请不要将此标记为重复,因为我已经完成了几乎所有帖子,我也看到用户有相同的问题和未解决。谢谢你的帮助
问题:如标题中所述;滚动时重复发布图像,但重复使用图像。我无法弄清楚为什么会重复。
public class postListBaseAdapter extends BaseAdapter {
private Context _context;
private JSONArray dataSet;
private LayoutInflater inflater;
public postListBaseAdapter(Context context){
_context = context;
inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try{
dataSet = new JSONArray(postdetails.getpostlist());
} catch (JSONException e) {
dataSet = new JSONArray();
} catch (Exception e) {
dataSet = new JSONArray();
}
}
public void changedata(){
try {
dataSet = new JSONArray(postdetails.getpostlist());
} catch (JSONException e) {
dataSet = new JSONArray();
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return dataSet.length();
}
@Override
public JSONObject getItem(int position) {
try {
return dataSet.getJSONObject(position);
} catch (JSONException e) {
return new JSONObject();
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final JSONObject jsob = getItem(position);
final ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.customview_postlist, null);
holder.userimage = (ImageView) convertView.findViewById(R.id.userimage);
holder.username = (TextView) convertView.findViewById(R.id.username);
holder.uniqueid = (TextView) convertView.findViewById(R.id.useruniquename);
holder.posts = (TextView) convertView.findViewById(R.id.posts);
holder.remainingtime = (TextView) convertView.findViewById(R.id.remainingtime);
holder.postimage = (ImageView) convertView.findViewById(R.id.post_image);
holder.comments = (TextView) convertView.findViewById(R.id.comments);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
try {
holder.comments.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(_context, Commentpage.class);
try {
i.putExtra("post_id", jsob.getString("post_id"));
} catch (JSONException e) {
e.printStackTrace();
}
_context.startActivity(i);
}
});
holder.username.setText(jsob.getString("name"));
holder.uniqueid.setText(jsob.getString("username"));
holder.posts.setText(jsob.getString("post"));
String Scount = jsob.getString("comment_count");
int count = Integer.parseInt(Scount);
holder.comments.setText(jsob.getString("comment_count")+" Comment"+(count>1?"s":""));
try{
if(jsob.getString("user_image").equalsIgnoreCase("null")){
holder.userimage.setImageDrawable(_context.getResources().getDrawable(R.drawable.default_profile));
}else{
Picasso.with(_context)
.load(postURLS.BASEURL+jsob.getString("user_image"))
.transform(new CircleTransform())
.placeholder(_context.getResources().getDrawable(R.drawable.default_profile))
.error(_context.getResources().getDrawable(R.drawable.default_profile))
.into(holder.userimage);
}
}catch(JSONException e){
Log.i("first exception", e.toString());
holder.userimage.setImageDrawable(_context.getResources().getDrawable(R.drawable.default_profile));
}
if(!jsob.getString("post_image").equalsIgnoreCase("null")){
Picasso.with(_context)
.load(postURLS.BASEURL+jsob.getString("post_image"))
.placeholder(_context.getResources().getDrawable(R.drawable.placeholder_postimage))
.error(_context.getResources().getDrawable(R.drawable.placeholder_postimage))
.into(holder.postimage);
holder.postimage.setVisibility(View.VISIBLE);
}
holder.postimage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(_context, Commentpage.class);
try {
i.putExtra("post_id", jsob.getString("post_id"));
} catch (JSONException e) {
e.printStackTrace();
}
_context.startActivity(i);
}
});
int time =0;
if((time = Integer.parseInt(jsob.getString("remaningtime")))!=0){
long calculatedtime = TimeUnit.SECONDS.toDays(time);
if(calculatedtime > 0){
holder.remainingtime.setText(String.valueOf(calculatedtime)+"D left");
}else if ((calculatedtime = TimeUnit.SECONDS.toHours(time))>0) {
holder.remainingtime.setText(String.valueOf(calculatedtime)+"h left");
}else if((calculatedtime = TimeUnit.SECONDS.toMinutes(time))>0){
holder.remainingtime.setText(String.valueOf(calculatedtime)+"m left");
}else if((calculatedtime = TimeUnit.SECONDS.toSeconds(time))>0){
holder.remainingtime.setText(String.valueOf(calculatedtime)+"s left");
}
}else{
convertView.setVisibility(View.GONE);
holder.remainingtime.setVisibility(View.INVISIBLE);
}
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
OttoBusProvider.getInstance().post((int)position);
// produceDeleteEvent();
}
}, TimeUnit.SECONDS.toMillis(Integer.parseInt(jsob.getString("remaningtime"))));
} catch (JSONException e) {
Log.i("exception", "caught exception while creating convertview in postlist");
Log.i("exception", e.toString());
holder.username.setText(" ");
holder.uniqueid.setText(" ");
holder.posts.setText(" ");
return convertView;
}
return convertView;
}
static class ViewHolder{
private ImageView userimage;
private TextView username;
private TextView uniqueid;
private TextView posts;
private TextView remainingtime;
private ImageView postimage;
private TextView comments;
}