I'm trying to extend a BaseAdapter to make my custom listview. But it says:
method call expected` in the getCount() method.
I know it's an Array, so how do I fetch it? I'm basically a nube in this field. Here is my code I have tried to apply:
public class customAdapter extends BaseAdapter implements View.OnClickListener {
private Context context;
private String[] restaurant;
private String[] address;
private String[] time;
private static LayoutInflater layoutInflater = null;
public Resources res;
int image[];
ListModel tempvalues = null;
int i=0;
public customAdapter(MainActivity mainActivity, String[] rest, String[] add,String[] tim,int[] img){
restaurant = rest;
context=mainActivity;
address= add;
time = tim;
image = img;
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return restaurant.length(); // Method call expected.
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView resname;
TextView resadd;
TextView restim;
ImageView imageView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowview;
rowview = layoutInflater.inflate(R.layout.custom_list_view_for_restaurant_names,null);
holder.resname = (TextView)rowview.findViewById(R.id.resnameTextView);
holder.resadd = (TextView)rowview.findViewById(R.id.address_textview);
holder.restim = (TextView)rowview.findViewById(R.id.time_textview);
holder.imageView = (ImageView)rowview.findViewById(R.id.imageView);
holder.resname.setText(restaurant[position]);
holder.resadd.setText(address[position]);
holder.restim.setText(time[position]);
holder.imageView.setImageResource(image[position]);
rowview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return rowview;
}
#Override
public void onClick(View v) {
}
}
Change
return restaurant.length();
to
return restaurant.length;
restaurant is a Array not a List
And one more thing I would like to suggest you,
Do not pass these arrays separately, because that will logically break the code, if some array size becomes less than the restaurant array.
I suggest you to use a model,
eg:
public class Event {
private String restaurant;
private String address;
private String time;
private int image;
//getters and setters
}
and pass a list of Event s
like below,
private List<Event> events;
public customAdapter(MainActivity mainActivity, List<Event> events){
this.events = events;
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return events.size(); // Method call expected.
}
#Override
public Object getItem(int position) {
return events.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
and access that,
Event event = events.get(position);
holder.resname.setText(event.getResturant());
holder.resadd.setText(event.getAddress());
holder.restim.setText(event.getTime());
holder.imageView.setImageResource(event.getImage());
And also,
You should not inflate the ListView Elements all the time you should reuse the already inflated elements.
View view = convertView;
if (view == null) {
// if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list_view_for_restaurant_names,null);
}
Related
My Adapter.
public class customDecisionAdapter extends BaseAdapter {
Context context;
private ArrayList<String> list1;
private ArrayList<String> list2;
public customDecisionAdapter(Context context, ArrayList<String>list1, ArrayList<String>list2) {
this.context= context;
this.list1= list1;
this.list2= list2;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View convertView = view;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customedecisio,viewGroup,false);
}
TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);
// Verify value of position not greater than size of ArrayList.
if(position < list1.size())
t1.setText(list1.get(position));
if(position< list2.size())
t2.setText(list2.get(position));
return convertView;
}
#Override
public int getCount()
{
if(list1.size() < list2.size())
return list2.size();
else
return list1.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
Also when first arraylist size is more than second arraylist size, second arraylist size is duplicating. Or when second arraylist size is more than first arraylist size, first arraylist is duplicating. How to fix. Help me. Thank you very much.
Try below solution.
I think there is a problem with your both if conditions.
here you only handle IF part. you should handle ELSE part also.
NOTE: The adapters are built to reuse Views when a View is scrolled so that is no longer visible, it can be used for one of the new Views appearing.
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View convertView = view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customedecisio, viewGroup, false);
}
TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);
// Verify value of position not greater than size of ArrayList.
if (position < list1.size()) {
t1.setText(list1.get(position));
} else {
t1.setText("");
}
if (position < list2.size()) {
t2.setText(list2.get(position));
} else {
t2.setText("");
}
return convertView;
}
You can use custom ArrayList and getter, setter method for that. Don't use two separate arrayLists. Make one model class as below: -
public class Model {
private String Disease;
private String Total;
public Model(String disease, String total) {
Disease = disease;
Total = total;
}
public String getDisease() {
return Disease;
}
public void setDisease(String disease) {
Disease = disease;
}
public String getTotal() {
return Total;
}
public void setTotal(String total) {
Total = total;
}
}
Use List<Model> list = new Arraylist<>();
**You should be using LinkedHashSet if you want to avoid duplicates and maintain order.**
//Your Wrapper Class
public class Model {
private String Disease;
private String Total;
public Model(String disease, String total) {
Disease = disease;
Total = total;
}
public String getDisease() {
return Disease;
}
public void setDisease(String disease) {
Disease = disease;
}
public String getTotal() {
return Total;
}
public void setTotal(String total) {
Total = total;
}
}
//Your Adapter
public class customDecisionAdapter extends BaseAdapter {
Context context;
private LinkedHashSet<Model> list1;
public customDecisionAdapter(Context context, LinkedHashSet<Model> list1) {
this.context= context;
this.list1= list1;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View convertView = view;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customedecisio,viewGroup,false);
}
TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);
// Verify value of position not greater than size of ArrayList.
t1.setText(list1.get(position).getDisease());
t2.setText(list1.get(position).getTotal());
return convertView;
}
#Override
public int getCount()
{
return list1.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
// Inside your activity onCreate method
LinkedHashSet<Model> listset = new LinkedHashSet<Model>();
listset.add(new Model("Malaria","200"));
listset.add(new Model("Cold","200"))
...//add more items here
//Then call your adapter and pass this list
customDecisionAdapter mAdapter = new customDecisionAdapter(listset,this);
You could just use Set as it behaves more less like a list but does not allow duplicates.
public class customDecisionAdapter extends BaseAdapter {
Context context;
private Set<String> set = new TreeSet<String>;
public customDecisionAdapter(Context context, ArrayList<String>list1, ArrayList<String>list2) {
this.context= context;
this.set.addAll(list1);
this.set.addAll(list2);
}
// rest of the code
Sorry just a noob here, my boss handed this to me from the former developer and I don't anything about android. My question is how do I pass extra dynamic values inside getView > onClick method so that accessible to me.
categoryCursorAdaptor
class categoryCursorAdaptor extends BaseAdapter {
Context context;
String[] data;
private static LayoutInflater inflater = null;
public categoryCursorAdaptor(Context context, String[] data) {
this.context = context;
this.data = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null) vi = inflater.inflate(R.layout.item_category, null);
TextView text = (TextView) vi.findViewById(R.id.specialCatItemName);
text.setText(data[position]);
Button Button1= (Button) vi.findViewById(R.id.btnSpecialView);
Button1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//Access extra values here
}
});
return vi;
}
}
CategoryActivity
String[] catValsArr = new String[arrayCatVals.size()];
catValsArr = arrayCatVals.toArray(catValsArr);
lvSpecialCatList = (ListView) findViewById(R.id.lvSpecialCatList);
lvSpecialCatList.setAdapter(new categoryCursorAdaptor(this, catValsArr));
You already have position inside getView(....) so use that to get proper data from String Array
String myData = data[position]; //Use myData
under onClick(...)
I have passed the value from fragment to mainactivity while click the button in fragment class. my main activity have one fragment and one horizontal listview with base adapter.my horizontal listview have one imageview and one textview. i want to set the value and image in particular horizontal listitem. if i have to click the first item plus button in fragment class the value and image want to set first item of horizontal listview. please suggest me.my horizontal listview adapter code here. thanks in advance.`
public class HorizontalAdapter extends BaseAdapter {
private Context context;
int value = 0;
int a[];
public HorizontalAdapter(Context c, int value) {
context = c;
this.value = value;
}
#Override
public int getCount() {
return 10;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.horizantal_image, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.title);
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
textView.setText(String.valueOf(value));
// imageView.setImageResource(image);
System.out.println(value);
System.out.println(position);
System.out.println(convertView);
System.out.println(parent);
// textView.setText(dataObjects[position]);
// imageView.setImageResource(imageid[position]);
return convertView;
}
}
You have to Pass some array of object or ArrayList According to your requirement and change the value of specific item of array and pass that to your adapter. You can create one Model Class in which you can define text and image like this
public class ListModel{
private String imageUrl;
private String title;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
And inside your BaseAdapter you have to use ArrayList of ListModel like this:
ArrayList<ListModel> listData = new ArrayList<ListModel>();
And you have to set content of listData from some where like this
ListModel listModelData = new ListModel();
// Add Content for specific object
listModelData.setTitle("your title");
listModelData.setImageUrl("your image url");
// Add this object in listview
listData.add(listModelData);
After that you have to pass this list in your BaseAdapter like this:
public class HorizontalAdapter extends BaseAdapter {
private Context context;
private int value = 0;
private ArrayList<ListModel> listData;
public HorizontalAdapter(Context c, int value, ArrayList<ListModel> listData) {
context = c;
this.value = value;
this.listData= listData;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.horizantal_image, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.title);
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
textView.setText(String.valueOf(listData.get(position).getImageUrl()));
// imageView.setImageResource(image);
System.out.println(String.valueOf(listData.get(position).getTitle()));
System.out.println(position);
System.out.println(convertView);
System.out.println(parent);
if (listData.get(position).getImageUrl()!=null){
imageView.setImageResource(listData.get(position).getImageUrl());
}
if (listData.get(position).getTitle()!=null){
textView.setText(listData.get(position).getTitle());
}
return convertView;
}
}
And to update specific position of ArrayList you have to do this:
listData.get(0).setImageUrl("Updated Image url");
listData.get(0).setTitle("Updated Title ");
And to set the adapter in your listview you have to do this from your activity:
HorizontalAdapter mAdapter = new HorizontalAdapter (mCtx, value,listData );
mListViews.setAdapter(mAdapter);
And to update the Listview you have to call notifyDataSetChanged after updating the content of ArrayList like this:
if (mAdapter !=null){
mAdapter .notifyDataSetChanged();
}
I am developing a hangman game for android, I am struggling with setting the gameplay rules for the onclick listener as gridview.getItem returns an object not a char value.
custom grid view
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private String[] items;
LayoutInflater inflater;
public CustomGridAdapter(Context context, String[] items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell, null);
}
Button button = (Button) convertView.findViewById(R.id.grid_item);
// text is set on button through a string array
button.setText(items[position]);
return convertView;
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int position) {
return items[position];
}
#Override
public long getItemId(int position) {
return position;
}
}
You should modify your code as here
#Override
public String getItem(int position) {
return items[position];
}
You can still use the BaseAdapter by changing the getItem as
#Override
public String getItem(int position) {
return items[position];
}
Or you can use an ArrayAdapter
If your items are strings then why not do:
#Override
public String getItem(int position) {
return items[position];
}
I might be wrong with this I'm new at android programming
I'm having a problem with returning my view to a listview with my custom adapter.
This is what I have in my main class which extends ListActivity.
lv = getListView();
ListAdapater itemAdapter = new ItemAdapter(MainActivity.this,itemList)
lv.setAdapter(itemAdapter);
And here's what I have for my custom adapter named ItemAdapter:
public class ItemAdapter extends BaseAdapter {
private static LayoutInflater inflater;
private Context context;
private ArrayList<HashMap<String,String>> newsItem;
public ItemAdapter(Context a, ArrayList<HashMap<String, String>> item){
super();
this.context = a;
this.newsItem = item;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
if(view==null){
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.news_item_layout,parent,false );
}
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView date = (TextView)vi.findViewById(R.id.date); // date
HashMap<String,String> nItem = newsItem.get(position);
title.setText(nItem.get(MainActivity.TITLE));
date.setText(nItem.get(MainActivity.DATE));
//UrlImageViewHelper.setUrlDrawable(thumb_image, nItem.get(MainActivity.IMAGE));
return vi;
}
I'm not sure why this adapter does not return any view at all? I've tried it with a SimpleAdapter with the same data and it works. I need to create a custom adapter to handle image data.
Any help would be greatly appreciated. Thanks
Based on count only your views will added to your listview, So change your getCount() method as
#Override
public int getCount() {
return newsItem.size();
}
And change
if(view==null)
to
if(vi==null)
I hope this will help you.
Here in your adapter you return 0 value.
#Override
public int getCount() {
return 0;
}
change it to
#Override
public int getCount() {
return item.size();
}
and in getView method change
if(view==null)
to
if(vi==null)
#Override
public int getCount() {
return newsItem.size();
}
#Override
public Object getItem(int i) {
return newsItem.get(i);
}
#Override
public long getItemId(int i) {
return newsItem.get(i).hashCode();
}
and
if(vi==null)
// try this way,hope this will help you...
public class ItemAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> newsItem;
public ItemAdapter(Context a, ArrayList<HashMap<String, String>> item) {
super();
this.context = a;
this.newsItem = item;
}
#Override
public int getCount() {
return newsItem.size();
}
#Override
public Object getItem(int i) {
return newsItem.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.news_item_layout,null,false);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.date = (TextView) convertView.findViewById(R.id.date);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.title.setText(newsItem.get(position).get(MainActivity.TITLE));
holder.date.setText(newsItem.get(position).get(MainActivity.DATE));
//UrlImageViewHelper.setUrlDrawable(thumb_image, nItem.get(MainActivity.IMAGE));
return view;
}
class ViewHolder{
TextView title;
TextView date;
}
}