Images in gridview change their position on scrolling in android - android

I am trying to fill my Gridview with some TextViews and Imageviews. I did it successfully, but when I scroll down in the grid, the images change their postion. I have read some other questions related to the same issue; like this one question, but it did not help me.
Aadapter
public class ItemListBaseAdapter extends BaseAdapter {
private static ArrayList<ItemDetails> itemDetailsrrayList;
private LayoutInflater l_Inflater;
private Context context;
// private OnClickListener listner;
public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
itemDetailsrrayList = results;
l_Inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Object getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.activity_gridview_items,
null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName = (TextView) convertView
.findViewById(R.id.tv_item_title);
holder.txt_itemDescription = (TextView) convertView
.findViewById(R.id.tv_item_detail);
holder.txt_itemPrice = (TextView) convertView
.findViewById(R.id.tv_item_price);
holder.itemImage = (ImageView) convertView
.findViewById(R.id.iv_item_image);
holder.txt_itemName
.setText(itemDetailsrrayList.get(position).getName());
holder.txt_itemDescription.setText(itemDetailsrrayList.get(position)
.getItemDescription());
holder.txt_itemPrice.setText("Rs "
+ itemDetailsrrayList.get(position).getPrice());
holder.itemImage.setImageBitmap(itemDetailsrrayList.get(position)
.getImage());
return convertView;
}
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
TextView txt_itemPrice;
ImageView itemImage;
Button likeBtn;
Button addBtn;
}
}

You need to call findViewById when convertView == null.
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.activity_gridview_items, parent, false);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView
.findViewById(R.id.tv_item_title);
holder.txt_itemDescription = (TextView) convertView
.findViewById(R.id.tv_item_detail);
holder.txt_itemPrice = (TextView) convertView
.findViewById(R.id.tv_item_price);
holder.itemImage = (ImageView) convertView
.findViewById(R.id.iv_item_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

Related

Custom listview throwing a null pointer exception

I am creating a custom ListView in android. The problem am facing is in my adapter code. It throws a NullPointerException even though everything else seems in order. Sample code below:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private List<LearnLangs> langsList;
private LayoutInflater layoutInflater;
public CustomAdapter(Context ctx, List<LearnLangs> langsList) {
this.ctx = ctx;
this.langsList = langsList;
}
#Override
public int getCount() {
return langsList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public ImageView img;
public TextView title;
public TextView sub;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
}
return convertView;
}
}
Exception thrown:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.joey.sunshine.CustomAdapter.getView(CustomAdapter.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15761)
What seems to be the problem?
You are getting exception because you are not using view which you are inflating at all. Change your code of getview() method as per below :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View mView = convertView;
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (mView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) mView.findViewById(R.id.list_img);
holder.title = (TextView) mView.findViewById(R.id.list_title);
holder.sub = (TextView) mView.findViewById(R.id.list_subT);
mView.setTag(holder);
}
else
{
holder = (ViewHolder) mView.getTag();
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
}
return mView;
}
Just change this line :
layoutInflater.inflate(R.layout.custom_list, parent, false);
to this:
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
EDIT
public AdapterConstructor() {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //move this line inside the constructor
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
return convertView;
}
Just change your getView() method by below :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
return convertView;
}

Android Custom ListAdapter nullpointerException

İts my custom adapter. not wrong but nullexceptionpointer.its my LogCat eror,NUllPointException eror. where is wrong this code?
public class CustomAdapter extends ArrayAdapter<ARACBILGISI1>{
private LayoutInflater vi ;
private Context context;
private ArrayList<ARACBILGISI1> liste;
public CustomAdapter(Activity context, int textViewResourceId, ArrayList<ARACBILGISI1> liste) {
super(context, R.layout.araclistegorselim, liste);
vi= LayoutInflater.from(context);
this.liste = liste;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.araclistegorselim, parent,false);
holder.aracplaka = (TextView) convertView.findViewById(R.id.plaka);
holder.aracadres = (TextView) convertView.findViewById(R.id.kullanim);
holder.X = (TextView) convertView.findViewById(R.id.x);
holder.Y = (TextView) convertView.findViewById(R.id.y);
holder.hizz = (TextView) convertView .findViewById(R.id.hiz);
holder.mudurluk = (TextView) convertView.findViewById(R.id.mud);
holder.aracresmi = (ImageView) convertView.findViewById(R.id.Aracresmi);
convertView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
ARACBILGISI1 arac = liste.get(position);
holder.aracplaka.setText(arac.getPlaka());
holder.aracadres.setText(arac.GETKULLANIM());
holder.X.setText(arac.GETX());
holder.Y.setText(arac.GETY());
holder.hizz.setText(arac.GETHIZ());
holder.mudurluk.setText(arac.GETMUD());
holder.aracresmi.setImageDrawable(context.getResources().getDrawable(arac.GETRESIM()));
return convertView;
}
static class ViewHolder{
public TextView aracplaka,aracadres,X,Y,hizz,mudurluk;
public ImageView aracresmi;
}
}
my data,Activity add,its my LogCat eror,NUllPointException eror. where is wrong this code?
private ArrayList<ARACBILGISI1> getARACBILGISI(){
ArrayList<ARACBILGISI1> takimList = new ArrayList<ARACBILGISI1>();
takimList.add(new ARACBILGISI1("fg", "fgh", "asd", "23","aaaa","bbbb",R.drawable.yesil));
takimList.add(new ARACBILGISI1("fg", "fgh", "asd", "23","aaaa","bbbb",R.drawable.yesil));
takimList.add(new ARACBILGISI1("fg", "fgh", "asd", "23","aaaa","bbbb",R.drawable.yesil));
takimList.add(new ARACBILGISI1("fg", "fgh", "asd", "23","aaaa","bbbb",R.drawable.yesil));
return takimList;
}
1.Rename your Adapter better
Change this
public class adapter extends ArrayAdapter<ARACBILGISI1>{
to
public class CustomAdapter extends ArrayAdapter<ARACBILGISI1>{
2.Change this
ArrayAdapter<ARACBILGISI1> aracadap = new adapter(this, R.layout.araclistegorselim, getARACBILGISI());
to
CustomAdapter aracadap = new CustomAdapter(this, R.layout.araclistegorselim, getARACBILGISI());
3.You can initialize LayoutInflater in constructor of adapter class
private LayoutInflater vi ;
public adapter(Activity context, int layout, ArrayList<ARACBILGISI1> liste) {
super(context, layout, liste);
this.liste = liste;
this.context = context;
vi= LayoutInflater.from(context);
}
4.Use a ViewHolder pattern. It will improve performance
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = vi.inflate(R.layout.araclistegorselim, parent,false);
holder.aracplaka = (TextView) convertView.findViewById(R.id.plaka);
holder.aracadres = (TextView) convertView.findViewById(R.id.kullanim);
holder.X = (TextView) convertView.findViewById(R.id.x);
holder.Y = (TextView) convertView.findViewById(R.id.y);
holder.hizz = (TextView) convertView .findViewById(R.id.hiz);
holder.mudurluk = (TextView) convertView.findViewById(R.id.mud);
holder.aracresmi = (ImageView) convertView.findViewById(R.id.Aracresmi);
convetView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
ARACBILGISI1 arac = liste.get(position);
holderaracplaka.setText(arac.getPlaka());
holderaracadres.setText(arac.GETKULLANIM());
holderX.setText(arac.GETX());
holder.Y.setText(arac.GETY());
holder.hizz.setText(arac.GETHIZ());
holder.mudurluk.setText(arac.GETMUD());
holderaracresmi.setImageDrawable(arac.GETRESIM());
return convertView;
}
static class ViewHolder
{
TextView aracplaka,aracadres,X,Y,hiz,mudurluk;
ImageView aracresmi;
}
As a side note you need not inflate the layout in getView again since you have
super(context, layout, liste);
and do check ArrayAdapter constructors #
http://developer.android.com/reference/android/widget/ArrayAdapter.html
try this :
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.araclistegorselim, parent,false);
}
ARACBILGISI1 arac = liste.get(position);
TextView aracplaka = (TextView) convertView
.findViewById(R.id.plaka);
TextView aracadres = (TextView) convertView
.findViewById(R.id.kullanim);
TextView X = (TextView) convertView
.findViewById(R.id.x);
TextView Y = (TextView) convertView
.findViewById(R.id.y);
TextView hizz = (TextView) convertView
.findViewById(R.id.hiz);
TextView mudurluk = (TextView) convertView
.findViewById(R.id.mud);
ImageView aracresmi = (ImageView) convertView
.findViewById(R.id.Aracresmi);
aracplaka.setText(arac.getPlaka());
aracadres.setText(arac.GETKULLANIM());
X.setText(arac.GETX());
Y.setText(arac.GETY());
hizz.setText(arac.GETHIZ());
mudurluk.setText(arac.GETMUD());
aracresmi.setImageDrawable(context.getResources().getDrawable(
arac.GETRESIM()));
return convertView ;
}

Different event by clicking on different buttons in listview by row position

I have a listview with a custom adapter like this:
I would like to set different actions for every button in listview, for example switching by position in the listview. How can i do that?
This is my custom adapter:
public class Customadapter extends ArrayAdapter<Formazionicontainer>{
Context context;
public Customadapter(Context context, int resourceId,
List<Formazionicontainer> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Formazionicontainer rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_group, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.text1);
holder.imageView = (ImageView) convertView.findViewById(R.id.image1);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
One solution is use the ViewHolder as the Button click listener:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Formazionicontainer rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_group, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.text1);
holder.imageView = (ImageView) convertView.findViewById(R.id.image1);
holder.button = (ImageView) convertView.findViewById(R.id.button1);
// Set the ViewHolder to handle the Button click
holder.button.setOnClickListener(holder);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
// Update the holder item
holder.item = rowItem;
return convertView;
}
private class ViewHolder implements View.OnClickListener {
TextView txtTitle;
ImageView imageView;
Button button;
Formazionicontainer item;
#Override
public void onClick(View view) {
// Handle the button click here...
// The item attribute contains the information about the Button's item clicked.
}
}
Lets say you have an array that you passed to your list view adapter:
String[] values = {"Inter","Juventus"};
then you can do something like:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(position){
case 0: // Inter
break;
case 1: // Juventus
break;
}
}
});
//EDIT
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Formazionicontainer rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_group, null);
holder = new ViewHolder();
holder.button = (Button) convertView.findViewById(R.id.button1);
holder.button.setOnClickListener(holder);
holder.txtTitle = (TextView) convertView.findViewById(R.id.text1);
holder.imageView = (ImageView) convertView.findViewById(R.id.image1);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.item = rowItem;
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
/* you can also add on click listener this way to your views
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivityName.this,
"I am here at position: " + position,
Toast.LENGTH_LONG).show();
}
});
*/
return convertView;
}
private class ViewHolder implements View.OnClickListener {
Button button;
Formazionicontainer item;
TextView txtTitle;
ImageView imageView;
#Override
public void onClick(View view) {
// Your things here
}
}

getview is not called in BaseAdapter

I have a problem with my custom adapter. the method getView() is not called. I searched on my friends "google" and "stackoverflow" but anything fix my problem.
public class custom_adapter extends BaseAdapter {
private List<Activities> listData;
private LayoutInflater layoutInflater;
public custom_adapter(Context context,
List<Activities> listActivity) {
this.listData = listActivity;
layoutInflater = layoutInflater.from(context);
Log.v("BaseAdapter", "custom_adapter");
}
#Override
public int getCount() {
Log.v("BaseAdapter - getCount", String.valueOf(listData.size()));
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("BaseAdapter", "getView");
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_adapter, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.title);
holder.ratingBar = (RatingBar) convertView.findViewById(R.id.averageRatingBarActivitySearch);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Activities newsItem = (Activities) listData.get(position);
holder.name.setText(newsItem.getName());
holder.ratingBar.setRating(newsItem.getAverageMark());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getPictureActivityString());
}
return convertView;
}
public static class ViewHolder {
TextView name;
RatingBar ratingBar;
ImageView imageView;
}
}
Furthermore, GetCount is not null !
I will be very glade if you can help me
Regards
K.L
For the purpose of handling List of some objects, extending generic ArrayAdapter may be better choice instead of just BaseAdapter. It will handle a lot of things for you. Your whole adapter class could look like.
public class ActivitiesAdapter extends ArrayAdapter<Activities>
{
private LayoutInflater layoutInflater;
public ActivitiesAdapter(Context context, List<Activities> objects)
{
super(context, 0, objects);
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("BaseAdapter", "getView");
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_adapter, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.title);
holder.ratingBar = (RatingBar) convertView.findViewById(R.id.averageRatingBarActivitySearch);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Activities newsItem = getItem(position);
holder.name.setText(newsItem.getName());
holder.ratingBar.setRating(newsItem.getAverageMark());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getPictureActivityString());
}
return convertView;
}
public static class ViewHolder {
TextView name;
RatingBar ratingBar;
ImageView imageView;
}
}
and of course you have to set this adapter to your ListView instance. Like
ActivitiesAdapter adapter = new ActivitiesAdapter(this, yourActivitiesList);
ListView listView = (ListView)findViewById(R.id.my_list_id);
listView.setAdapter(adapter);
or simply setListAdapter(adapter); if you are using ListActivity or ListFragment;
I had the same problem. Maybe, the problem is that you don't notify your adapter that you've changed data in the adapter. Try this code in your adapter:
#Override
public int getCount() {
return data.size();
}
And call notifyDataSetChanged(); when you change the data in Adapter.

Create different number of items inside a ListView Android

In this adapter, you'll have to fill your items to an ID already created in the layout.
How do I proceed to create new Imageview(or other) inside this adapter, when each item inside my importet list have a different amount of images or text for each listrow?
Example:
I want to fill inn a X number of pictures where the black/white boxes are, depending on each item.
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}

Categories

Resources