So today I created Expandable List View, everything went well without any error, excepts this in Adapter.java
I'm getting error: Operator '==' cannot be applied to 'android:view.View', 'int' in the if loop just below String title line.
Adapter.java
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String title = (String)this.getGroup(groupPosition);
if(convertView == 0){
LayoutInflater layoutInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.parent_531_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.heading_item);
textView.setTypeface(null, Typeface.BOLD);
textView.setText(title);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String title = (String)this.getChild(groupPosition, childPosition);
if (convertView == 0){
LayoutInflater layoutInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_531_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.child_item);
textView.setText(title);
return convertView;
}
So if anyone can correct my errors and tell me what I did wrong, I would appreciate it!
View is an object and there is not logical(possible to compare this againt an int)
try checking if is a null reference or not.
if (convertView == null){
...
}
View here is the expandable list view item, you cann't directly compare it to any integer like you have done.
If you want to check whether view is empty than check like this :
if (convertView == null){
//your code here
}
Hope it helps !
Related
So I am attempting to set a different layout resource for my first ListItem element using this code:
int type;
#Override
public int getItemViewType(int position) {
if(position==0) {
type = R.layout.queue_item_next;
} else {
type = R.layout.queue_item;
}
return type;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) inflater.inflate(getItemViewType(position), parent, false);
}
This code works however with some unexpected behavior. For some reason the last element of the ListView is also being set to have this alternate layout and I have no idea why.
What could cause this to happen?
Thanks.
Issue is coming due getItemViewType() is returning value greater than number of view types. You can use bellow code working perfectly fine for me.
#Override
public int getItemViewType(int position) {
if(position==0) {
return 0;
}
return 1;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int type = getItemViewType(position);
if(type == 0)
convertView = (View) inflater.inflate(R.layout.first_layout, parent, false);
else
convertView = (View) inflater.inflate(R.layout.second_layout, parent, false);
}
}
Note : If only first view is different then best option is to use headerView using function listview.addHeaderView() function using this link
because you are using if(convertView == null) {}
that means if previous view is available in memory use that one else create a new one. So, sometimes on scroll fast/slow depends on resources available of phone it varies.
to solve this problem dont check if(convertView == null)
use like this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) inflater.inflate(getItemViewType(position), parent, false);
}
In ImageView, my images are being shuffled but not the text. This is my getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mainActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.item_people, parent, false);
} else {
v = (View) convertView;
}
TextView textView = (TextView) v.findViewById(R.id.name);
ImageView imageView = (ImageView) v.findViewById(R.id.image);
if (list.get(position).getClass() == SearchData.Video.class) {
SearchData.Video video = (SearchData.Video) list.get(position);
textView.setText(video.getVideoName());
if (video.getCoverPicture().length > 0)
imageView.setBackground(mainActivity.Base64toImage(video.getCoverPicture()[0].getImg()));
} else if (list.get(position).getClass() == SearchData.Actor.class) {
SearchData.Actor actor = (SearchData.Actor) list.get(position);
textView.setText(actor.getFirstName());
if (actor.getPicture().length > 0)
imageView.setBackground(mainActivity.Base64toImage(actor.getPicture()[0].getImg()));
}
return v;
}
I am setting image and text of actors. When I scroll down then up the images have shuffled, but not text. Why?
Did you tried the same with ViewHolder
Normally when ever getView() method is called, the gridview/ listview will automatically notified as a change, So that is the reason for your image shuffling s / changing in your gridview.
So, try to implement your Gridview with ViewHolder pattern and avoid those shuffling s / re-orderings
here is the sample code for view holder pattern implementation.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
// The convertView argument is essentially a "ScrapView" as described is Lucas post
// http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
// It will have a non-null value when ListView is asking you recycle the row layout.
// So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
// object item based on the position
ObjectItem objectItem = data[position];
// assign values if the object is not null
if(objectItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.textViewItem.setText(objectItem.itemName);
viewHolder.textViewItem.setTag(objectItem.itemId);
}
return convertView;
}
your ViewHodler should be like this
// ViewHolder.
// caches our TextView
static class ViewHolderItem {
TextView textViewItem;
}
This is not the exactly same to your question, but you can edit the above logic for your way.
Hope it Helps :)
I am a beginner. And I've tried a lot but I don't know how I can do this:
I have an ArrayAdapter. That displays all items and if position == 3 it should display also a label.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (position == 3){
rowView = inflater.inflate(R.layout.event_date, parent, false);
// set text of label ...
// now I want to display also the normal item
} else {
rowView = inflater.inflate(R.layout.events_list, parent, false);
// set text of item ...
}
return rowView;
}
It displays the label but I don't know how I can also show the normal item if it shows the label. How can I do this?
Try using a new layout which would contains both the event_date and events_lists
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (position == 3){
rowView = inflater.inflate(R.layout.event_date_plus_events_list, parent, false);
// set text of label ...
} else {
rowView = inflater.inflate(R.layout.events_list, parent, false);
}
// set text of item ...
return rowView;
}
I have question. I am programming List Adapter in Java / Android. I override method which returns View (it's expendable list view from Clicked item). The problem is that:
Log.i shows me:
05-17 14:04:50.494: I/createTextView(21790): 0 06 Salame
05-17 14:04:50.504: I/createTextView(21790): 1 11 Pancetta
But TextViews looks both:
1 11 Pancetta
1 11 Pancetta
It looks like TextView refers to last TextView. How to fix that?
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.e("SecondLevelAdapter", "getGroupView");
TextView tv = new TextView(OrderFirstGridPage.this);
if((myArrayProductList.get(groupPosition).get(0).getFlgZlozony() == 1)) {
Log.i("createTextView", String.valueOf(counterProductName)+" "+myArrayProductList.get(groupPosition).get(counterProductName).toString());
tv.setText(myArrayProductList.get(groupPosition).get(counterProductName).toString());
counterProductName++;
}
tv.setPadding(22, 7, 7, 7);
tv.setGravity(Gravity.LEFT);
tv.setTextAppearance(OrderFirstGridPage.this, R.style.TextLarge);
tv.setTextSize(25);
return tv;
}
I think using LayoutInflater would be abetter way:
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.yourLayout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.yourTextViewId);
if((myArrayProductList.get(groupPosition).get(0).getFlgZlozony() == 1)) {
Log.i("createTextView", String.valueOf(counterProductName)+" "+myArrayProductList.get(groupPosition).get(counterProductName).toString());
tv.setText(myArrayProductList.get(groupPosition).get(counterProductName).toString());
counterProductName++;
}
tv.setPadding(22, 7, 7, 7);
tv.setGravity(Gravity.LEFT);
tv.setTextAppearance(OrderFirstGridPage.this, R.style.TextLarge);
tv.setTextSize(25);
return convertView;
}
I have a gridview in Android which is populated by a custom view which inherits from imagebuttons.
What I would like to do is modify the size of the imagebuttons inside the gridview as well as their padding between cells.
Here is my getView() code. I've tried using layouts but can't because I don't have access to the parent view.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (image == null)
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.dollar);
ExpenseIcon ic = new ExpenseIcon(context);
ic.setImageBitmap(image);
return ic;
}
How do I do that?
You can set the with with setColumnWidth(int) or set the column count by calling setNumColumns(int) depending on what you extactly trying. By the way you can also set that values by xml (android:columnWidth and android:numColumns).
For the spacing you can use android:horizontalSpacing or setHorizontalSpacing(int) and android:verticalSpacing or setVerticalSpacing(int).
For controling the highe you need to change your getView function:
public YourConstructor() {
// inflater should be a private member variable
inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// other code...
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
// create new one and put it into convertView. e.g.:
convertView = new ExpenseIcon(context);
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(HEIGHT,WIDTH);
convertView.setLayoutParams(lp);
}
// set the image to the currient convertView. e.g.:
((ExpenseIcon)convertView).setImageBitmap(image);
return convertView;
}
Create an XML and inflate.I gave sample to use in getview...Set imagesize in the layout..
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageResource(list.get(position).getIconid());
return convertView;
}
static class ViewHolder {
ImageView image;
}