getView-method in customized Adapter is not called - java

im working on a tank-game. in this tank-game i want to display the items which the player can use(shild,firework,atombomb)i want to realise this with a listview and a arraya-dapter. i've made a customized ArrayAdapter like i found it in much answers but it didnt work. The list is not there after i set the adapter. After i debug the project i came to the conclusion that the getView-method in the adapter class isnt called
why?
This is my adapter class
public class Adapter extends ArrayAdapter {
private Context context;
private ArrayList<Item> items;
public Adapter(#NonNull Context context, ArrayList<Item> items) {
super(context, R.layout.listview_item);
this.context = context;
this.items = items;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_item, parent, false);
viewHolder.imageView = convertView.findViewById(R.id.imageView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageBitmap(items.get(position).getImageView());
return convertView;
}
static class ViewHolder{
ImageView imageView;
}
}
Here i want to set the adapter
public void createList(ListView listView){
Adapter adapter = new Adapter(context,tanks.get(currentTank).getItems());
listView.setAdapter(adapter);
}

public Adapter(#NonNull Context context, ArrayList<Item> items) {
super(context, R.layout.listview_item);
this.context = context;
this.items = items;
}
Instead of this try this :
public MyCustomListAdapter(Context context,int resource, ArrayList<Item> items)
{
super(context,resource,list);
this.context= context;
this.resource=resource;
this.list=list;
}
Also in your main class write this code:
Adapter adapter = new Adapter(context,R.layout.<Thename of your layout>,tanks.get(currentTank).getItems());

Related

Holder vs RecylerView.ViewHolder

I have ListView with Adapter, and also use holder. but later I read about recyclerView.ViewHolder and now confused, Is it different with the one I've been using right now? I mean for the optimization purpose, I want to know if using holder only is not good enough without using recyclerView.
public class NewsAdapter extends ArrayAdapter<News> {
Context context;
List<News> myList;
public NewsAdapter(Context context, int resource, List<News> objects) {
super(context, resource, objects);
this.context = context;
this.myList = objects;
}
#Override
public News getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_news, null);
holder = new NewsAdapter.Holder();
holder.title = (TextView)convertView.findViewById(R.id.textViewTitle);
holder.datePosted = (TextView)convertView.findViewById(R.id.textViewDate);
holder.imageView = (ImageView)convertView.findViewById(R.id.imageViewThumbnailpic);
convertView.setTag(holder);
}else{
holder = (Holder)convertView.getTag();
}
News news = getItem(position);
holder.title.setText(news.getTitle());
holder.datePosted.setText(news.getDatePost());
Picasso.with(context)
.load(news.getImgUrl())
.placeholder(R.drawable.coin25)
.error(R.drawable.hnbspic)
.into(holder.imageView);
return convertView;
}
private class Holder{
ImageView imageView;
TextView title;
TextView datePosted;
}
}
It's better to use Recyclerview because it has been optimized for various scenarios and not just for View holder pattern like it give the option for determining how your item should be laid out or like what should be the animation or custom drawing in each item.You can read more this medium post

setAdapter compiler error from listview to my custom adapter

I'm not sure what's wrong with this segment of code:
mylistview = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this, rowItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
The line causing an error is mylistview.setAdapter(adapter).
CustomAdapter is a java class with the constructor defined as so:
Context context;
List<RowItem> rowItems;
CustomAdapter(Context context, List<RowItem> rowItems){
this.context = context;
this.rowItems = rowItems;
}
The error I'm getting is:
'setAdapter(android.widget.ListAdapter)' in 'android.widget.ListView' cannot be applied to '(com.name.app.CustomAdapter)'
I have no idea what's causing this error and would greatly appreciate some insight as to why I can't set the adapter of my custom adapter to the listview. Thanks.
Try this one
public class SpinnerAdapter extends BaseAdapter{
Context context;
ArrayList<String> list;
LayoutInflater layoutInflater;
SpinnerAdapter(Context context, ArrayList<String> list)
{
layoutInflater = LayoutInflater.from(context);
this.list = list;
this.context = context;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public Object getItem(int position)
{
return list.get(position);
}
#Override
public long getItemId(int position)
{
return list.indexOf(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = layoutInflater.inflate(R.layout.spinner_s,null);
TextView client = (TextView) convertView.findViewById(R.id.client);
client.setText(list.get(position).toString());
return convertView;
}
The setAdapter requires android.widget.ListAdapter. It looks like you CustomAdapter class needs to implement the ListAdapter interface

Data between activities

I have a special class, a custom adapter for my ListView and I need to get some data from another Activity. But my implementation of the method GetIntent()GetExtras() isn't working. What is wrong?
Here is my custom adapter code:
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.ColorTextButton);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageViewIcon);
TextView textView1 = (TextView) rowView.findViewById(R.id.HelpButton);
textView.setText(values[position]);
String s = values[position];
System.out.println(s);
if (s.equals("Monday")) {
imageView.setImageResource(R.drawable.arrow2);
textView.setBackgroundColor(Color.YELLOW);
} else if (s.equals("Wednesday")) {
imageView.setImageResource(R.drawable.arrow2);
textView1.setBackgroundColor(Color.GREEN);
} else if (s.equals("Friday")) {
imageView.setImageResource(R.drawable.arrow2);
} else {
imageView.setImageResource(R.drawable.arrow);
}
return rowView;
}
}
If you want the data to be accessable From a few activities you should think about use a singleton data Class. so in stead of trying to get pass the data from your adapter to a second activity both the adaper and the Activity use the same method to get the data class
public class DataProvider {
private static DataProvider instance;
public static DataProvider getInstance()
{
if(null == instance){instance = new DataProvider();}
return instnace;
}
public String[] getObjects(){
return this.myStringArray;
}
// add more methods in here to retrieve and count your data as you need
}
then in your Activity you can
// somewhere
myStingArrayInMyActivity = DataProvider.getInstance().getObjects()
also in you adapter
you can do
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// your code
textView.setText(DataProvider.getInstance.getObjects[position]);
String s = DataProvider.getInstance.getObjects[position];
/// your code
return rowView;
}
}

Sliding menu adapter(android)

Sorry for my english. I use SlideMenu libruary and i want use new font for textview, it old version use:
((ListView) ((Activity) context).findViewById(R.id.sidemenulistobject)).setAdapter(
new ArrayAdapter<Object>(
context,
R.layout.sidemenu_item,
R.id.textSlide,
items
)
);
But this i cant get my textSlide and set new font. Now i add array adapter and set this in listView. This is my all code:
menu = new SlidingMenu(context);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setShadowWidth(15);
menu.setFadeDegree(1.0f);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.attachToActivity((Activity) context, SlidingMenu.SLIDING_WINDOW);
menu.setBehindWidth(400);
menu.setMenu(R.layout.sidemenu);
//add item in list view
ArrayList<String> itemsObj = new ArrayList<String>();
itemsObj.add("Новости");
itemsObj.add("События");
itemsObj.add("Наше меню");
itemsObj.add("Фотографии");
itemsObj.add("Видеозаписи");
itemsObj.add("Контакты");
itemsObj.add("Мой профиль");
//get sidemenulistobject
ListView lv = ((ListView) ((Activity) context).findViewById(R.id.sidemenulistobject));
//add adapter
SlideAdapter adapter = new SlideAdapter((Activity) context, R.layout.sidemenu_item, itemsObj);
lv.setAdapter(adapter);
This is my SlideAdapter
public class SlideAdapter extends ArrayAdapter<MenuCategoryObject>{
ArrayList<String> listItems;
int Resourse;
Context context;
LayoutInflater vi;
private ImageLoader imageLoader;
public SlideAdapter(Context context, int resource, ArrayList<String> listItems) {
super(context, resource);
this.listItems = listItems;
Resourse = resource;
this.context = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = vi.inflate(Resourse, null);
holder = new ViewHolder();
Typeface face=Typeface.createFromAsset(context.getAssets(), "font/AvenirNext-Medium.ttf");
holder.textSlide = (TextView) convertView.findViewById(R.id.textSlide);
holder.textSlide.setTypeface(face);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textSlide.setText(listItems.get(position));
return convertView;
}
static class ViewHolder {
public TextView textSlide;
}
}
But my item list view dont show. I dont know why. Please help
the problem is with your adapter. Since you are not providing the dataset to the super constructor, you have to override getCount and return its size. Add
public int getCount() {
return listItems.size();
}
to your adapter

why every example that extends ArrayAdapter saves the list as private?

why in every example i see that extends the class ArrayAdapter it overrides the constructor that accepts T[] objects and references its own private variable to that and uses the referenced variable in the override of getView?
how can this be dynamic adding of objects?
will values be updated when a new item is added to the ArrayAdapter using add method?
can i instead of implementing my own adapter just use addheaderview in listview?
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// change the icon for Windows and iPhone
String s = values[position];
if (s.startsWith("iPhone")) {
imageView.setImageResource(R.drawable.no);
} else {
imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}

Categories

Resources