There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. And the ListView is visibility。
I had call setListAdapter on the ListView,and the xml is match_parent.
This is my Fragment.
mNewsList = infoBean.getNews();
if (mNewsList != null) {
mNewsAdapter = new NewsAdapter();
mListView.setAdapter(mNewsAdapter);
mNewsAdapter.notifyDataSetChanged();
}
} else {//load more
List<HomeBean.NewsBean> news = infoBean.getNews();
if (mNewsList != null) {
mNewsList.addAll(news);
mNewsAdapter.notifyDataSetChanged();
}
This is my Adapter:
private class NewsAdapter extends BaseAdapter {
#Override
public int getCount() {
return mNewsList.size();
}
#Override
public HomeBean.NewsBean getItem(int position) {
return mNewsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MyApplication.getmContext(), R.layout.item_list, null);
holder = new ViewHolder();
holder.ivIcon = convertView.findViewById(R.id.iv_item);
holder.tvTitle = convertView.findViewById(R.id.tv_item_list_title);
holder.content = convertView.findViewById(R.id.tv_item_list_content);
holder.tvTime = convertView.findViewById(R.id.tv_time);
holder.author = convertView.findViewById(R.id.tv_author);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText(getItem(position).getTitle());
holder.content.setText(getItem(position).getContent());
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
String itemTime = dataFormat.format(getItem(position).getCreate_at() * 1000);
holder.tvTime.setText(itemTime);
holder.author.setText(getItem(position).getSection_name());
if (getItem(position).getCover_pic().startsWith("http:")) {
picUrl = getItem(position).getCover_pic();
} else {
picUrl = RBConstants.SERVER_PIC + getItem(position).getCover_pic();
}
HttpLoader.getInstance(MyApplication.getmContext()).display(holder.ivIcon, picUrl);
return convertView;
}
}
This is my holder.
class ViewHolder {
public ImageView ivIcon;
public TextView tvTitle;
public TextView tvTime;
public TextView content;
public TextView author;
}
this is because mListView.setAdapter(mNewsAdapter); is never executed and of cource there is no need to call mNewsAdapter.notifyDataSetChanged(); immediatelly after calling mListView.setAdapter(mNewsAdapter);
ListView is old, please switch to RecyclerView.
Related
How to fix with Gridview getview position Too little cause repetitive problems
it is my code
class MyAdapter extends BaseAdapter {
private ArrayList<UserInforData> myData;
public MyAdapter(ArrayList<UserInforData> data){
myData = data;
}
public void updateData(ArrayList<UserInforData> data){
myData = data;
mAdapter.notifyDataSetChanged();
}
#Override
public int getCount() {
return myData.size();
}
#Override
public Object getItem(int position) {
return myData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
UserInforData d= myData.get(position);
Log.d(TAG, "getView: "+d.getUserPic());
Log.d(TAG, "getView: "+myData.toString());
ViewHolder holder = null;
if (convertView == null)
{
convertView = LayoutInflater.from(mActivity).inflate(R.layout.islivelayout,null);
holder = new ViewHolder();
holder.liveImg =(ImageView) convertView.findViewById(R.id.liveimg);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
// if(d.isLive==1){
Log.d(TAG, "isLive: "+d.getUserPic());
String reSizeUrl = JinShanImageScaleUtil.replaceJinShanUrl(d.getUserPic(),30,30);
GlideUtil.loadUrl(mActivity, d.getUserPic(), R.drawable.lobby_defult_profile, holder.liveImg, false, false);
// }
return convertView;
}
}
i try to getPosition is at 5 or 6
but i sliding will repeat the same picture
please tell how to fix
it seems there's no problem with your code, check the data , by the way, you have not used reSizeUrl
i have done custom spinner with custom adapter in it but getting some default text everytime how to avoid this issue everytime it should only display 0th position text on spinner:
CourseAdapter courseAdapter = new CourseAdapter(mContext);
customViewHolder.spinner.setAdapter(courseAdapter);
while CourseAdapter.java
public class CourseAdapter extends BaseAdapter {
public static final String TAG=CourseAdapter.class.getName();
List<String> courses =new ArrayList<>(); // code to get the courses ArrayList
Context context;
private void fillCourses()
{
for(int i =0;i<10;i++)
{
if(i == 0)
{
courses.add("Choose");
}
else
{
courses.add("courses:"+i);
}
}
}
public CourseAdapter(Context context)
{
this.context = context;
fillCourses();
}
#Override
public int getCount() {
return courses.size();
}
#Override
public Object getItem(int position) {
return courses.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderFilterItem viewHolder;
if (convertView == null) {
Log.d(TAG, "fillCourses size:" + courses.size());
convertView = LayoutInflater.from(context)
.inflate(R.layout.layout_customspinner, parent, false);
viewHolder = new ViewHolderFilterItem();
viewHolder.textView = (TextView) convertView.findViewById(R.id.txtViewFilterItem);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkboxFilterItem);
//store the holder with the view.
convertView.setTag(viewHolder);
return convertView;
}
else
{
Log.d(TAG, "convertview != null:" + courses.size());
viewHolder = (ViewHolderFilterItem) convertView.getTag();
}
if(courses != null && courses.size() > 0) {
String itemText = courses.get(position);
if (itemText != null) {
viewHolder.textView.setText(itemText);
viewHolder.checkBox.setChecked(true);
}
if(position == 0)
{
viewHolder.checkBox.setVisibility(View.GONE);
}
}
return convertView;
}
private class ViewHolderFilterItem{
TextView textView;
CheckBox checkBox;}
}[![[why Filter item text with checkbox appearing ?][1]][1]
you have to use courseAdapter.notifyDataSetChanged() after setting customViewHolder.spinner.setAdapter(courseAdapter);
Hope this helps.
I have a list view that populates with information using a custom adapter, displaying a tree like structure. However, the 'Parent' level comments always repeat themselves. This is the code for my custom adapter:
class CommentListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public CommentListAdapter(CommentActivity commentActivity){
layoutInflater = (LayoutInflater) commentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return commentFeed.getCommentCount();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
class listViewHolder {
RelativeLayout spacerRelLayout;
TextView authorText;
TextView bodyText;
TextView timeText;
listViewHolder(View v) {
spacerRelLayout = (RelativeLayout) v.findViewById(R.id.spacerLayout);
authorText = (TextView) v.findViewById(R.id.authorTextComment);
bodyText = (TextView) v.findViewById(R.id.commentTextView);
timeText = (TextView) v.findViewById(R.id.timeTextComment);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItem = convertView;
listViewHolder holder;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.comment_item_layout, parent, false);
holder = new listViewHolder(listItem);
listItem.setTag(holder);
} else {
holder = (listViewHolder) listItem.getTag();
}
holder.bodyText.setText(Html.fromHtml(commentFeed.getComment(position).getBody()));
holder.timeText.setText(commentFeed.getComment(position).getTime());
holder.authorText.setText(commentFeed.getComment(position).getAuthor());
holder.spacerRelLayout.getLayoutParams().width = commentFeed.getComment(position).getLevel() *10;
holder.spacerRelLayout.invalidate();
return listItem;
}
}
How can this be fixed?
getItem should look like this:
#Override
public Object getItem(int position) {
return commentFeed.getComment(position);
}
Then inside your getView you do this:
Comment comment = (Comment) getItem(position);
holder.bodyText.setText(Html.fromHtml(comment.getBody()));
// etc..
I have a custom list view with image and text.Now whenever i open the list view initially the image is visible.Now when i scroll the list the images are being visible.Also i have put condition on which image should be displayed when,but in beginning all the images are same the images are only changed when i scroll the list.I know this issue is related to recycle of the list but i am not able to solve it.Please do help me
Code
public class ShareWithMeAdapter extends BaseAdapter {
private Context context;
private ArrayList<SharedWithMeModel> data;
public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, viewGroup, false);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvTitle.setText(data.get(i).getTitle());
if (data.get(i).getName().equals("")) {
holder.tvName.setText("Shared With " + data.get(i).getName());
} else {
holder.tvName.setText("Shared With " + data.get(i).getName());
}
holder.tvSharedOn.setText(data.get(i).getDate());
if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
holder.image.setBackgroundResource(R.drawable.image);
}
if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
holder.image.setBackgroundResource(R.drawable.docx);
}
if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
holder.image.setBackgroundResource(R.drawable.excel);
}
if (data.get(i).getExtension().equals(".pdf")) {
holder.image.setBackgroundResource(R.drawable.pdf);
}
// if (data.get(i).getExtension().equals(".ppt")) {
// holder.image.setBackgroundResource(R.drawable.);
// }
return view;
}
private class ViewHolder {
TextView tvTitle, tvName, tvSharedOn;
ImageView image;
}
}
Try this way,hope this will help you to solve your problem.
public class ShareWithMeAdapter extends BaseAdapter {
private Context context;
private ArrayList<SharedWithMeModel> data;
public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return data.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, null);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvTitle.setText(data.get(i).getTitle());
if (data.get(i).getName().equals("")) {
holder.tvName.setText("Shared With " + data.get(i).getName());
} else {
holder.tvName.setText("Shared With " + data.get(i).getName());
}
holder.tvSharedOn.setText(data.get(i).getDate());
if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
holder.image.setImageResource(R.drawable.image);
}else if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
holder.image.setImageResource(R.drawable.docx);
}else if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
holder.image.setImageResource(R.drawable.excel);
}else{
holder.image.setImageResource(R.drawable.pdf);
}
return view;
}
private class ViewHolder {
TextView tvTitle;
TextView tvName;
TextView tvSharedOn;
ImageView image;
}
}
try this , hope it might helpfull
replace all
holder.image.setBackgroundResource(R.drawable.filename);
with
holder.image.setImageResource(R.drawable.filename);
or change these methods
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
I'm developing an Android Application and I have a custom adapter for my ListView. I want to use a Pull to refresh library, such as this one. This works fine with an ArrayAdapter, but not with a custom adapter.
How can I get my pull to refresh working with a custom adapter?
Here my adapter code:
public class AdvertListAdapter extends BaseAdapter {
private AdvertListActivity activity;
private int currentFirstVisibleItem=0;
private int currentLastVisibleItem=8;
public AdvertListAdapter(AdvertListActivity activity) {
this.activity = activity;
}
public void setFirstVisibleItem(int currentFirstVisibleItem) {
this.currentFirstVisibleItem=currentFirstVisibleItem;
}
public void setLastVisibleItem(int currentLastVisibleItem) {
this.currentLastVisibleItem=currentLastVisibleItem;
}
#Override
public int getCount() {
return activity.getAdvertList().size();
}
#Override
public Advert getItem(int pos) {
return activity.getAdvertList().get(pos);
}
#Override
public long getItemId(int pos) {
return activity.getAdvertList().get(pos).getId();
}
private class ViewHolder {
public ImageView image;
public TextView title;
public TextView price;
public TextView size;
public TextView city;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.listview_advertlist, null);
view.image = (ImageView) convertView.findViewById(R.id.advertImage);
view.title = (TextView) convertView.findViewById(R.id.advertTitleText);
view.price = (TextView) convertView.findViewById(R.id.advertPriceText);
view.size = (TextView) convertView.findViewById(R.id.advertSizeText);
view.city = (TextView) convertView.findViewById(R.id.advertCityText);
convertView.setTag(view);
}else {
view = (ViewHolder) convertView.getTag();
}
Advert advert = activity.getAdvertList().get(position);
view.title.setText(advert.getTitle());
view.price.setText(String.valueOf(advert.getPrice()+"€"));
view.size.setText("Taille: "+advert.getSize());
view.city.setText(advert.getCity());
if(position <= currentFirstVisibleItem || position >= currentLastVisibleItem) {
view.image.setImageResource(R.drawable.defaultadvertimage);
ServiceHelper.loadAsyncImage(view.image, activity.getApplicationContext(), String.valueOf(advert.getId()+"-mini"), "advert");
}
return convertView;
}
}
Here my activity code:
list = (PullToRefreshListView) findViewById(R.id.advertList);
advertList = getAdvertList();
((PullToRefreshListView) list).setOnRefreshListener(new OnRefreshListener() {
#Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
list.setAdapter(adapter);