Reputation: 970
I have a listView
witch shows a list of news .when user reada news item, after it's appearance should get gray.so in listadapter
i check that a news is readed or not and if it is read layout 1 get load to an item and if not layout 2 get load to this item. the problem is : the top 10 first loaded item in litview
display perfect but when i scroll to load more item and back to the the op of listview
gray listview
item with layout 2 get changes to layout 1! here is my listadapter
code : what is the problem ?
package ir.whc.news.adapter;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.annotation.LayoutRes;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.orm.query.Condition;
import com.orm.query.Select;
import java.util.List;
import ir.whc.news.R;
import ir.whc.news.classes.MyApplication;
import ir.whc.news.classes.Utility;
import ir.whc.news.model.NewsItem;
import ir.whc.news.model.SeenItem;
/**
* Created by marzieh on 18/03/2016.
*/
public class NewsListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<NewsItem> newsItems;
private int node_id;
private int subdomain_id;
Context context;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader(true);
public NewsListAdapter(Activity activity, List<NewsItem> newsItems, int subdomain_id, int node_id) {
this.activity = activity;
this.newsItems = newsItems;
this.node_id = node_id;
this.subdomain_id = subdomain_id;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = activity;
}
@Override
public int getCount() {
return newsItems.size();
}
@Override
public NewsItem getItem(int position) {
return newsItems.get(position);
}
@Override
public long getItemId(int position) {
return newsItems.get(position).get_id();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("position is " ,String.valueOf(position));
final NewsItem item = newsItems.get(position);
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
if (IsReed(item.get_id(),item.get_termId()))
convertView = inflater.inflate(R.layout.item_news_iread, null);
else
convertView = inflater.inflate(R.layout.item_news, null);
if (imageLoader == null)
imageLoader = MyApplication.getInstance().getImageLoader(true);
LinearLayout parrentLayout = (LinearLayout) convertView.findViewById(R.id.parentL);
TextView titleHolder = (TextView) convertView.findViewById(R.id.text_title);
TextView summeryHolder = (TextView) convertView.findViewById(R.id.text_summery);
TextView dateHolder = (TextView) convertView.findViewById(R.id.text_date);
NetworkImageView thumbpicHolder = (NetworkImageView) convertView
.findViewById(R.id.image_news_thumb);
final ImageView image_bookmark = (ImageView) convertView.findViewById(R.id.image_bookmark);
titleHolder.setText(item.get_title());
summeryHolder.setText(item.get_summary());
imageLoader.get(item.get_imgPath(), imageLoader.getImageListener(thumbpicHolder,
R.drawable.ic_launcher,
android.R.drawable.ic_dialog_alert));
thumbpicHolder.setImageUrl(item.get_imgPath(), imageLoader);
thumbpicHolder.setVisibility(item.get_imgPath().trim().isEmpty() ? View.GONE : View.VISIBLE);
dateHolder.setText(Utility.GetFormattedDteTime(newsItems.get(position).get_datePublish()));
//checkBookmark
if (Select.from(NewsItem.class).where(Condition.prop("_id").eq(item.get_id())).list().size() > 0)
image_bookmark.setImageResource(R.drawable.ic_bookmarkfill);
else
image_bookmark.setImageResource(R.drawable.ic_bookmark_normal);
//add or delet from bookmark
image_bookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utility.Insert_bookmark("_id", item, image_bookmark, NewsItem.class);
}
});
/* Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_down);
convertView.startAnimation(animation);
*/
return convertView;
}
private boolean IsReed(int _id,int _term_id) {
////check is reed
if (SeenItem.find(SeenItem.class, "_id = ? and _nodeid = ? and _termid = ? and _domainid= ? ", String.valueOf(_id),
String.valueOf(node_id),
String.valueOf(_term_id),
String.valueOf(subdomain_id)).size() > 0) {
return true;
} else
return false;
}
}
Upvotes: 0
Views: 43
Reputation: 16607
Your getView of Adapter it's not properly written and your inflater & convertView not properly initialize following code is the right way to initialize your items :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.row_list_base, parent,false);
holder = new ViewHolder();
holder.videoImage = (ImageView)convertView.findViewById(R.id.image_video_preload);
holder.title = (TextView) convertView.findViewById(R.id.text_video_title);
holder.subtitle = (TextView) convertView.findViewById(R.id.text_video_subtitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ShortVideoModel item = getItem(position);
if (item != null) {
ImageLoaderHelper.displayImage(mContext, holder.videoImage, item.getImageUri());
if (!TextUtils.isEmpty(item.getTitle())) {
holder.title.setText(item.getTitle());
}
//... initialize rest of things
}
return convertView;
}
Upvotes: 1
Reputation: 1035
In Android Adpater follow the concept of reuse of views. Means If there are 5 views showing on screen then you scroll the list then 1st view will recycled to 6th view. That's why you getting this type of result. so Just remove following if condition in your code.
if (convertView == null)
Or you can use two type view concept BaseAdapter have two more methods like getViewType(int position)
and by this you can identify which type of view you want to render.
Upvotes: 3