Create different number of items inside a ListView Android - 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));
}
}

Related

Custom adapter smart scrolling

I am trying to implement my custom adapter but I am facing performance issues. It seems that every time I scroll the list it is loaded all over again. I want it to hold the view and display only the visible part. My list item has imageview and textview.
My code:
class CustomAdapter extends BaseAdapter {
private Context context;
private ArrayList<CounselorInfo> counselors;
private static LayoutInflater inflater = null;
CustomAdapter(Context context, ArrayList<CounselorInfo> counselors) {
this.context = context;
this.counselors = counselors;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return counselors.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
private class Holder
{
TextView textView;
ImageView imageView;
}#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView = inflater.inflate(R.layout.counselors_list, null);
holder.textView = (TextView) rowView.findViewById(R.id.nameCounselor);
holder.imageView = (ImageView) rowView.findViewById(R.id.imageCounselor);
holder.textView.setText(counselors.get(position).getName());
ImageLoadTask imageLoad = new ImageLoadTask(" http:// " + counselors.get(position).getImageURL(), holder.imageView);
imageLoad.execute();
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
return rowView;
}
}
Android studio gives suggestion on this row:
View rowView = inflater.inflate(R.layout.counselors_list, null);
it says: Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling.
But I use inner class Holder so I don't know how to solve it.
add this on getView():
final Holder holder;
if (convertView == null) {
holder = new Holder();
View rowView = inflater.inflate(R.layout.counselors_list, null);
holder.textView = (TextView) rowView.findViewById(R.id.nameCounselor);
holder.imageView = (ImageView) rowView.findViewById(R.id.imageCounselor);
convertView.setTag(holder);
} else
{
holder = (Holder) convertView.getTag();
}

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));

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.

Images in gridview change their position on scrolling in 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();
}

Categories

Resources