Custom adapter for ListView repeating objects - java

i've a custom adapter to populate an ListView, i have an ArrayList, when i execute the program the ListView is entire populated with the first object from arraylist.
I searched for an similar question here on stackoverflow and i didnt find a solution, plase, someone help me
Heres playerAdapter.java
public class playerAdapter extends ArrayAdapter<Player>{
private ArrayList<Player> entries;
private Activity activity;
public playerAdapter(Activity a, int textViewResourceId, ArrayList<Player> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.itemplayer, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.nomecharlist);
//holder.item2 = (TextView) v.findViewById(R.id.small);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Player custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getNomePlayer());
//holder.item2.setText(custom.getSecond());
}
return v;
}
}

I'm not sure what the exact problem is with your code, but I do know that's it's much too complicated. Try something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.itemplayer, null);
}
final Player custom = entries.get(position);
if (custom != null)
((TextView) convertView.findViewById(R.id.nomecharlist)).setText(custom.getNomePlayer());
return convertView;
}

Related

ListView ViewHolder item child View visibility changing

I'm having a ListView with a custom ArrayAdapter. The problem is that I cannot hide (setVisibility(View.GONE)) TextView in some elements of choice. Here is a minimized-readable summary part of my code:
public class ListViewAdapter extends ArrayAdapter<ItemModel> {
private int listItemLayout;
static class ViewHolder {
private TextView description;
}
public ListViewAdapter(Context context, int layoutId, List<ItemModel> itemList) {
super(context, layoutId, itemList);
this.listItemLayout = layoutId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemModel item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.description = convertView.findViewById(R.id.text_desription);
viewHolder.description.setText(item.getDescription());
if (TextUtils.isEmpty(item.getDescription())) {
viewHolder.description.setVisibility(View.GONE);
}
return convertView;
}
}
What am I doing wrong here? How can I hide a view inside item in ListView if its content String is empty?
Add else part of if condition to change visibility from GONE to VISIBLE as in below code.
public class ListViewAdapter extends ArrayAdapter {
private int listItemLayout;
static class ViewHolder {
private TextView description;
}
public ListViewAdapter(Context context, int layoutId, List itemList) {
super(context, layoutId, itemList);
this.listItemLayout = layoutId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemModel item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.description = convertView.findViewById(R.id.text_desription);
viewHolder.description.setText(item.getDescription());
if (TextUtils.isEmpty(item.getDescription())) {
viewHolder.description.setVisibility(View.GONE);
}else{
viewHolder.description.setVisibility(View.VISIBLE);
}
return convertView;
}
}
OR try if (item.getDescription()==null || item.getDescription().trim().length() == 0)
instead of if (TextUtils.isEmpty(item.getDescription()))
Check any white space should not be in your text. Try this-
if (TextUtils.isEmpty(item.getDescription().trim()) ||item.getDescription().trim().length()==0 ) {
viewHolder.description.setVisibility(View.GONE);
}
else{
viewHolder.description.setVisibility(View.VISIBLE);
}

categorise listView items into different sections - Android

in my listView i'm trying to divide (categorize) my data in to different section say i have a list of states and i wanna categorize them according to their Countries but what i have right now is categorizing my data Alphabetically and i have no clue how it is happening and how can i achieve what i want. can anyone guide me here ?
my adapter :
public class MyAdapter extends BaseAdapter implements StickyListHeadersAdapter {
private String[] countries;
private LayoutInflater inflater;
private ArrayList<States> statesArray;
public MyAdapter(Context context , ArrayList<States> states) {
inflater = LayoutInflater.from(context);
satesArray = states;
countries = context.getResources().getStringArray(R.array.all_countries);
}
#Override
public int getCount() {
return statesArray.size();
}
#Override
public Object getItem(int position) {
return statesArray.get(position).getName();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_view_row, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.listViewTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(statesArray.get(position).getName());
return convertView;
}
#Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
if (convertView == null) {
holder = new HeaderViewHolder();
convertView = inflater.inflate(R.layout.sticky_header, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
//set header text as first char in name
String headerText = "" + countries[position];
holder.text.setText(headerText);
return convertView;
}
#Override
public long getHeaderId(int position) {
// return the first character of the country as ID because this is what headers are based upon
return satesArray.get(position).getName().subSequence(0, 1).charAt(0);
}
class HeaderViewHolder {
TextView text;
}
class ViewHolder {
TextView text;
}
}
UPDATE :
what i want is right here but instead of Alphabets at Section there would be Country name and at rows there would be states of respective Country :
righ here

Android - Images changing on GridView when scrolling

I am trying to create a custom adapter for a shop element in my Android app but every time I scroll down the list the images seem to change their position, I have had a look around on here already but suggestions that I have found and tried have been to no avail.
Each item has a title, an image (being loading via an AsyncTask or retrieved from the LRUCache) and a price of the item below is the code I use to generate the adapter.
public class ShopAdapter extends BaseAdapter {
public Context context;
public ArrayList<ShopItem> shopItemList;
public ImageCache imageCache;
String imageToLoad;
Bitmap shopImage;
public ShopAdapter(Context context, ArrayList<ShopItem> shopItemList) {
this.context = context;
this.shopItemList = shopItemList;
imageCache = new ImageCache(this.context);
}
#Override
public int getCount() {
return shopItemList.size();
}
#Override
public Object getItem(int position) {
return shopItemList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.shop_list, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.textView3);
holder.image =(ImageView) convertView.findViewById(R.id.imageView);
holder.price = (TextView) convertView.findViewById(R.id.textView4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(shopItemList.get(position).getTitle());
shopImage = imageCache.getBitmapFromMemoryCache(shopItemList.get(position).getImage());
if(shopImage != null) {
holder.image.setImageBitmap(shopImage);
} else {
new LoadImage(context, holder.image).execute(imageToLoad);
}
holder.price.setText(shopItemList.get(position).getPrice());
return convertView;
}
static class ViewHolder {
TextView title;
ImageView image;
TextView price;
}
}
Any help would be very much appreciated!

Using adapter for listview

I have an arraylist that needs to be updated using the adpater. I dont have any textviews for it. Here is my code -
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private ArrayList<MyClass> favouriteMessageList;
private MyAdapter(Activity activity, ArrayList list) {
favouriteMessageList = list;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
MyClass favouriteMessage = favouriteMessageList.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.messaging_favorites_layout, null);
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public void setData(ArrayList<MyClass> data) {
favouriteMessageList = data;
notifyDataSetChanged();
}
}
The setData method will receive an arraylist and I need to populate the view with my Arraylist. I am using a viewHolder. How do I achieve this?
Try like this:
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private ArrayList<MyClass> favouriteMessageList;
private MyAdapter(Activity activity, ArrayList list) {
favouriteMessageList = list;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
MyClass favouriteMessage = favouriteMessageList.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.messaging_favorites_layout, null);
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//Update Your Views Here
return convertView;
}
#Override
public int getCount() {
if (favouriteMessageList != null) return favouriteMessageList.size();
return 0;
}
public void setData(ArrayList<MyClass> data) {
favouriteMessageList = data;
notifyDataSetChanged();
}
}

change label text on button click inside a listview item

I have a listview item with a label and two buttons. I tried to change the label on button click. But it's changing text on another listview item. Not the label with button. I did this using a custom list adapter. I tried it like following,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MenuItem listItem = objects.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, null);
btnPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
cartQtyTextView.setText("" + ++i);
}
});
}
}
How may I fix this?
You might want to create holder.
I didn't include your menu item code due to i didn't see you got use it.
public class Holder {
ButtonRectangle buttonPlus;
TextView cartQtyTextView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get Holder
final Holder holder = new Holder();
// Change Layout
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.menu_list_item, null);
// Find Control
holder.buttonPlus = (ButtonRectangle)view.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView)view.findViewById(R.id.cartQtyTextView);
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
return view;
}
This is because you are setting the listener inside the if(convertView==null).
So the listener is only set once a view is created, but when you scroll, the list view is reusing a hidden item, but it keep the first assigned listener since the convertView is not null.
You need to set your onClickListener outside the if. And it is better if you use a holder for better performances
public class ExampleAdapter extends ArrayAdapter<MenuItem> {
private Activity activity;
private int resource;
private List<MenuItem> objects;
public ExampleAdapter(Activity activity, int resource, List<MenuItem> objects) {
super(activity, resource, objects);
this.activity = activity;
this.resource = resource;
this.objects= objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MenuItem listItem = objects.get(position);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater li = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(resource, parent, false);
holder.labelTextView= (TextView) convertView.findViewById(R.id.labelTextView);
holder.btnPlus= (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
holder.labelTextView.setText("" + ++i);
}
});
return convertView;
}
static class ViewHolder {
TextView labelTextView;
ButtonRectangle btnPlus;
}
}

Categories

Resources