GridView will disappear upon scrolling - android

When I scroll my GridView up and down, the gridview will start lose its children and eventually (intermittently) the whole gridview will dissapear on bottom of page if I done too many scroll.
Here is my adapter code:
public class Adapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<Item> list;
public Adapter(Context _context, List<Item> _list){
this.context = _context;
this.list = _list;
this.inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return this.list.size() - 1;
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public long getItemId(int arg0) {
return 0;
}
public class ViewHolder {
ImageView imageView;
TextView itemLabel;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, .., null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(..);
holder.itemLabel = (TextView)convertView.findViewById(..);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Item item = list.get(position + 1);
holder.itemLabel.setText(item.getTitle());
holder.imageView.setImageDrawable(item.getImage());
return convertView;
}
}
What could be wrong, as my adapter is a standard adapter?

Related

display video from database into gridview

I would like to display video from database into a grid view i managed to do it with images but have no idea how to do it with a videoview , i would like to do the same but for videos
public class Photoadapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<Photo> photoList;
#Override
public int getCount() {
return photoList.size();
}
#Override
public Object getItem(int position) {
return photoList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
ImageView imageView;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imageViewList);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Photo photo = photoList.get(position);
final byte[] photoImage = photo.getImage();
final Bitmap bitmap = BitmapFactory.decodeByteArray(photoImage, 0, photoImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
public Photoadapter(Context context, int layout, ArrayList<Photo> photoList) {
this.context = context;
this.layout = layout;
this.photoList = photoList;
}
}

DIsplay first letter of string to another textview in Android

I am displaying a listview in which, the row item has two textview. The second textview is for the name of companies and the first textview is for the starting letter of the companies. How this can be achieved ? Need help !!
public class ExampleAdapter extends BaseAdapter {
Context context;
ArrayList<ExamplePojo> items = new ArrayList<>();
public ExampleAdapter(Context context, ArrayList<ExamplePojo> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.indexOf(items.get(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.example_row_item, null);
holder = new ViewHolder();
holder.txtSentence = (TextView) convertView.findViewById(R.id.txtSentence);
holder.txtInitialLetter = (TextView) convertView.findViewById(R.id.txtInitialLetter);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().charAt(0));
return convertView;
}
public class ViewHolder{
TextView txtSentence, txtInitialLetter;
}
}
Here is code for showing first character in first textview and complete name in other textview.Also put below lines in if(convertView== null) block
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().substring(0, 1));

How to change row view in listview when clicking on item

Is it possible to change row view when I'm clicking on it? For example I have listView with rows where some short information shown, and when after clicking on row detailed view will show instead of old row.
I tried to use addView in my listView, but "listview addView(View, int) is not supported in AdapterView" error appeared.
try something like that:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<UserPojo> mList;
public MyAdapter(Context context, List<UserPojo> users) {
this.mContext = context;
this.mList = users;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public UserPojo getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final UserPojo user = mList.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_ad, null);
holder = new ViewHolder();
holder.pic = (CircleImageView) convertView.findViewById(R.id.profile_imageView);
holder.name = (TextView) convertView.findViewById(R.id.name_textView);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
if (user.isClicked) {
holder.pic.setVisibility(View.VISIBLE);
holder.name.setVisibility(View.GONE);
} else {
holder.pic.setVisibility(View.GONE);
holder.name.setVisibility(View.VISIBLE);
}
holder.name.setText("text");
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.setClicked(true);
notifyDataSetChanged();
}
});
return convertView;
}
private static class ViewHolder {
CircleImageView pic;
TextView name;
}
}

content of listview does not appear

my code is running without error but the custom Listview does not display the values .
my code is running without error but the custom Listview does not display the values .
this is my code:
the Adapter
public class Adapter extends BaseAdapter{
ArrayList<mealItems> itemList;
public Activity context;
public LayoutInflater inflater;
public DiseasesAdapter(Activity context,int x,ArrayList<mealItems> itemList) {
super();
this.context = context;
this.itemList = itemList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object getItem(int position) {
return itemList.get(position);
}
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
ImageView img;
TextView Title;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.disease_row, null);
holder.Title = (TextView) convertView.findViewById(R.id.Name);
holder.img = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
Items bean = (Items) itemList.get(position);
holder.Title.setText(bean.getTitle());
holder.img.setImageResource(bean.getImage());
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
}
Your getCount() method returns a 0, this should return the size of the itemList instead.
i.e return itemList.size()
Try this :
this.itemList=new ArrayList<mealItems>();
this.itemList.addAll(itemList);
And of course, the getCount() method to return itemList.size()

How to return strings to a custom ListView?

How would I go about inserting this code into a ListView in android?
List<YouTubeVideo> videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout);
for (YouTubeVideo youtubeVideo : videos) {
System.out.println(youtubeVideo.getWebPlayerUrl());
System.out.println("Thumbnails");
for (String thumbnail : youtubeVideo.getThumbnails()) {
System.out.println("\t" + thumbnail);
}
System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl());
System.out.println("************************************"); }
}
}
Would I create a custom adapter and use getView()? If so how would it look?
Yes, I would create a custom adapter and use getView(). Here is some sample code for a row layout with a TextView and an ImageView:
private static class MyListAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Item> items;
private LayoutInflater mInflater;
public MyListAdapter(Context context, ArrayList<Item> items)
{
this.context = context;
this.items = items;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, parent, false);
}
Item item = items.get(position);
TextView tv = (TextView) convertView.findViewById(R.id.data_item);
tv.setText(item.getItem());
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1);
iv.setImageBitmap(item.getBitmap());
return convertView;
}

Categories

Resources