GridView's first Item is not showing up - android

I'm trying to add gridView and customize its adapter.
But the first item does not appear, and item click listener also not works.
Is my code incorrect?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.layout_mission_private_item, parent, false);
convertView.setLayoutParams(new AbsListView.LayoutParams(parent.getWidth() / 3, parent.getHeight() / 2));
text = (TextView) convertView.findViewById(R.id.tvContent);
convertView.setTag(text);
} else {
text = (TextView) convertView.getTag();
}
text.setText(mData.get(position).first);
if (mData.get(position).second)
text.setBackgroundColor(Color.GREEN);
return convertView;
}
edit: full source
public class MissionPrivateGvAdapter extends BaseAdapter {
private LayoutInflater mLayoutInflater;
public ArrayList<Pair<String, Boolean>> mData;
public MissionPrivateGvAdapter(LayoutInflater inflater, ArrayList<Pair<String, Boolean>> data) {
mLayoutInflater = inflater;
mData = data;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position).first;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.layout_mission_private_item, parent, false);
convertView.setLayoutParams(new AbsListView.LayoutParams(parent.getWidth() / 3, parent.getHeight() / 2));
text = (TextView) convertView.findViewById(R.id.tvContent);
convertView.setTag(text);
} else {
text = (TextView) convertView.getTag();
}
text.setText(mData.get(position).first);
if (mData.get(position).second)
text.setBackgroundColor(Color.GREEN);
return convertView;
}
}

The width and height of parent view is zero when you first invoke getView method.Maybe you can obtain screen content width and height to set their values.

Related

Android ListView Items Being Repeated

I have a list view that populates with information using a custom adapter, displaying a tree like structure. However, the 'Parent' level comments always repeat themselves. This is the code for my custom adapter:
class CommentListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public CommentListAdapter(CommentActivity commentActivity){
layoutInflater = (LayoutInflater) commentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return commentFeed.getCommentCount();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
class listViewHolder {
RelativeLayout spacerRelLayout;
TextView authorText;
TextView bodyText;
TextView timeText;
listViewHolder(View v) {
spacerRelLayout = (RelativeLayout) v.findViewById(R.id.spacerLayout);
authorText = (TextView) v.findViewById(R.id.authorTextComment);
bodyText = (TextView) v.findViewById(R.id.commentTextView);
timeText = (TextView) v.findViewById(R.id.timeTextComment);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItem = convertView;
listViewHolder holder;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.comment_item_layout, parent, false);
holder = new listViewHolder(listItem);
listItem.setTag(holder);
} else {
holder = (listViewHolder) listItem.getTag();
}
holder.bodyText.setText(Html.fromHtml(commentFeed.getComment(position).getBody()));
holder.timeText.setText(commentFeed.getComment(position).getTime());
holder.authorText.setText(commentFeed.getComment(position).getAuthor());
holder.spacerRelLayout.getLayoutParams().width = commentFeed.getComment(position).getLevel() *10;
holder.spacerRelLayout.invalidate();
return listItem;
}
}
How can this be fixed?
getItem should look like this:
#Override
public Object getItem(int position) {
return commentFeed.getComment(position);
}
Then inside your getView you do this:
Comment comment = (Comment) getItem(position);
holder.bodyText.setText(Html.fromHtml(comment.getBody()));
// etc..

How to make spinner show full items in Android

I have a problem with Android Spinner. I customised an adapter with a BaseAdapter. But when it show in the first time, it not show all items (I have only 6 items). Below is the screenshot
http://pik.vn/2014999186b9-448d-44ed-bd9b-c37484b368c6.png
And when I tap to the Spinner second time it show normally. Below is the screenshot. How can I show full item like that at the first time?
http://pik.vn/201454ed3e5b-4c67-4e9c-b8c6-feaf90c5dfb4.png
Update code:
Below is the adapter:
public class CategoryAdapter extends BaseAdapter implements SpinnerAdapter {
Context mContext;
List<CategoryModel> data;
int selectedCate;
boolean isSpinner;
Spinner spn;
public CategoryAdapter(Context context, List<CategoryModel> data, boolean isSpinner, Spinner spn) {
mContext = context;
selectedCate = 0;
this.data = data;
this.isSpinner=isSpinner;
this.spn = spn;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
if (position < data.size()) {
return data.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category_spinner, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_category_spinner_tv);
tvName.setSelected(spn.isSelected());
tvName.setText(data.get(position).getName());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category, parent, false);
ImageView img = (ImageView) convertView.findViewById(R.id.inflater_cate_image);
if (!data.get(position).getName().equals(parent.getContext().getString(R.string.browse))) {
Log.d("postion", "" + position);
UrlImageViewHelper.setUrlDrawable(img, data.get(position).getImage().getUrl());
} else {
img.setImageResource(R.drawable.ic_everything);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_cate_tv_name);
tvName.setText(data.get(position).getName());
return convertView;
}
public void swapView(int position) {
selectedCate = position;
}
}
and below is the code init Spinner in Activity
categoryAdapter = new CategoryAdapter(HomeActivity.this, result,true, mSpnActionbarCenterTitle);
mSpnActionbarCenterTitle.setAdapter(categoryAdapter);
The problem comes from the height of ImageView, it is wrap content and load via lazy loader, and the height of spinner not change when image loaded. My solution is fix the size of ImageView.

how to change the color of last listview adapter index

below is my ListView adapter class i want to change color of last position value, suppose my ListView shows 6 elements and i want to change the color of 6th element how i will do that??? i want to change only color of last element text
public class CustomAlertAdapter extends BaseAdapter{
TextView titlename;
Context ctx=null;
ArrayList<String> listarray=null;
private LayoutInflater mInflater=null;
public CustomAlertAdapter(Activity activty, ArrayList<String> list)
{
this.ctx=activty;
mInflater = activty.getLayoutInflater();
this.listarray=list;
}
#Override
public int getCount() {
return listarray.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
final ViewHolder holder;
if (convertView == null ) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.alertlistrow, null);
holder.titlename = (TextView)
convertView.findViewById(R.id.textView_titllename);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
String datavalue=listarray.get(position);
holder.titlename.setText(datavalue);
return convertView;
}
private static class ViewHolder {
TextView titlename;
}
}
Just change a property of the inflated view based on position in the data structure
public View getView(int position, View convertView, ViewGroup arg2) {
// inflate view
convertView = mInflater.inflate(R.layout.alertlistrow, null);
...
// modify view
if(position == listArray.size()-1){
convertView.setBackgroundColor(R.color.some_color_resource_id);
}
...
// return view
return convertView;
so long as that is completed before you return the view inside you getView function.
Worth remembering that is called for each element in the list, and the position is supplied to you.
Hope this helps
Try this inside getView(..)
if (position == listarray.size()-1) {
convertView.setBackgroundColor(Color.BLUE);
}
public View getView(int position, View convertView, ViewGroup arg2)
Here position is the position of particular item.
So check if position==total items(total number in your case could be size of arraylist)
and change color by
convertView.setBackground("Your Color here");
you could check the position and change the color accordingly. For instance:
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
final ViewHolder holder;
if (convertView == null ) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.alertlistrow, null);
holder.titlename = (TextView)
convertView.findViewById(R.id.textView_titllename);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String datavalue=listarray.get(position);
holder.titlename.setText(datavalue);
int color = (position == getCount() - 1) : Color.BLACK : Color.YELLOW;
holder.titlename.setTextColor(color)
return convertView;
}

android setOnItemClickListener not working on one row but ok in other row, using 2 row layouts

Im using a listView that have 2 layouts for the rows on one row the
setOnItemClickListener
but on the other row it doesnt recognize the taps,
public void initItemTable()
{
listViewItem = (ListView) getView().findViewById(R.id.listViewItem);
listViewItem.setAdapter(new PhoneItemAdapter(new ItemPhoneDataSource().getItems()));
listViewItem.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("mensa", "item index :"+arg2);
}
});
}
//
private class PhoneItemAdapter extends BaseAdapter {
final List<RowPhone> rows;//row
//data source, style
PhoneItemAdapter(List<ItemPhone> animals) {
rows = new ArrayList<RowPhone>();//member variable
//choose cell! iterator
for (ItemPhone item : animals) {
if (item.getType().equals("item")) {
rows.add(new RowItemPhone(LayoutInflater.from(getActivity()), item));
} else {
rows.add(new RowFolderPhone(LayoutInflater.from(getActivity()), item)); //imageRow!
}
}
}
//delegate
#Override
public int getViewTypeCount() {
return RowTypePhone.values().length;
}
#Override
public int getItemViewType(int position) {
//con cast
return ((RowPhone) rows.get(position)).getViewType();
}
public int getCount() {
return rows.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
//cambiado con cast!
return ((RowPhone) rows.get(position)).getView(convertView);
}
}
So the tap is detected when "item.getType()" is folder but not in item,
. Shall I include the code for RowItemPhone.java and RowFolderPhone.java?
so how to fix this tap problem?
thanks!
edit 1. RowPhone.java
public interface RowPhone {
public View getView(View convertView);
public int getViewType();
}
edit 2. RowFolder.java .. the one that have the tap detecting fine:
public class RowItemPhone implements RowPhone{
private final ItemPhone item;
private final LayoutInflater inflater;
public RowItemPhone(LayoutInflater inflater, ItemPhone animal) {
this.item = animal;
this.inflater = inflater;
}
//text doble
public View getView(View convertView) {
ViewHolder holder;
View view = null;
if (convertView == null) {
//ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, null);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, (ViewGroup) view, false);
holder = new ViewHolder(
(ImageView)viewGroup.findViewById(R.id.image_item),
(TextView)viewGroup.findViewById(R.id.title),
(TextView)viewGroup.findViewById(R.id.description));
viewGroup.setTag(holder); //pa q y como usa tag!
view = viewGroup;
} else {
view = convertView;
holder = (ViewHolder)convertView.getTag();
}
//setup
holder.imageView.setImageResource(item.getImageId());
holder.descriptionView.setText(item.getDescription());
holder.titleView.setText(item.getName());
return view;
}
public int getViewType() {
return RowTypePhone.DESCRIPTION_ROW.ordinal();
}
private static class ViewHolder {
final ImageView imageView;
final TextView titleView;
final TextView descriptionView;
private ViewHolder(ImageView imageView, TextView titleView, TextView descriptionView) {
this.imageView = imageView;
this.titleView = titleView;
this.descriptionView = descriptionView;
}
}
}
And here the RowFolderPhone.java ... showing fine, but not detecting taps:
public class RowFolderPhone implements RowPhone {
private final ItemPhone item;
private final LayoutInflater inflater;
public RowFolderPhone(LayoutInflater inflater, ItemPhone animal) {
this.item = animal;
this.inflater = inflater;
}
public View getView(View convertView) {
ViewHolder holder;
View view = null;
//we have a don't have a converView so we'll have to create a new one
if (convertView == null) {
// ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, null);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, (ViewGroup) view, false);
//use the view holder pattern to save of already looked up subview
holder = new ViewHolder((ImageView)viewGroup.findViewById(R.id.image),
(TextView)viewGroup.findViewById(R.id.title));
viewGroup.setTag(holder);
view = viewGroup;
} else {
//get the holder back out
holder = (ViewHolder)convertView.getTag();
view = convertView;
}
//actually setup the view
holder.imageView.setImageResource(item.getImageId());
holder.titleView.setText(item.getName());
return view;
}
public int getViewType() {
return RowTypePhone.IMAGE_ROW.ordinal();
}
private static class ViewHolder {
final ImageView imageView;
final TextView titleView;
private ViewHolder(ImageView imageView, TextView titleView) {
this.imageView = imageView;
this.titleView = titleView;
}
}
}
There are something wrong with your getItem method on PhoneItemAdapter. getItem method should return list item not position. So you should replace your code to
public RowPhone getItem(int position) {
return rows.get(position);
}
the problem was solved by setting descendantFocusability
in : row_item_phone.xml
<RelativeLayout ...
android:descendantFocusability="blocksDescendants"

How can i inflate three layouts in ListView?

I have a custom listView in which i want to inflate three different layouts.
I have seen many questions but they are all for odd and even position but in my listView the layout that should be inflated to the listView depends on other condition and its dynamic like
if (i == 0) i get the first Layout to be inflated and if (i==1) second one and so on,
The variable "i" is equal to the value I would be getting from my main Activity.
public class SocialListAdapter extends ArrayAdapter<Item> {
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
private int i;
public SocialListAdapter(Activity act, int resource, List<Item> arrayList) {
super(act, resource, arrayList);
// TODO Auto-generated constructor stub
this.activity= act;
this.items = arrayList;
this.row = resource;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return items.size();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
objBean = items.get(position);
i = objBean.getI();
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(i == 0){
view = inflater.inflate(R.layout.list, null);
} else if (i == 1){
view = inflater.inflate(R.layout.row1, null);
}
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
I tried this code but dint work...
I was getting an error when i scroll through the listView arrayoutofbondexception
getItemViewType has to return a number from 0 to getViewTypeCount.
Try this :
public class CustomAdapter extends BaseAdapter {
private ArrayList<String> comments;
Context mContext;
public CustomAdapter(Context context, ArrayList<String> comments) {
this.mContext = context;
this.comments = comments;
}
public View getView(final int position, View convertView, ViewGroup parent){
String item = comments.get(position);
if (getItemViewType(position) == 0) {
View v = convertView;
if (v == null) {
//GET View 1
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment2, null);
v = viewGroup;
}
//Fill Data for Ist view
TextView comm = (TextView) v.findViewById(R.id.comment);
comm.setText(item);
return v;
} else if (getItemViewType(position) == 1) {
View v = convertView;
if (v == null) {
//GET View 2
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment1, null);
v = viewGroup;
}
//Fill Data for IInd view
TextView comm = (TextView) v.findViewById(R.id.comment);
comm.setText(item);
return v;
} else {
//GET View 3
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment3, null);
v = viewGroup;
}
//Fill Data for IInd view
TextView comm = (TextView) v.findViewById(R.id.comment);
comm.setText(item);
return v;
}
}
#Override
public int getCount() {
return comments.size();
}
#Override
public Object getItem(int position) {
return comments.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if(position == 0)
return 0;
else if (position == 1)
return 1;
else
return 2;
}
}
You can optimize and make your scrolling smooth by defining view holder :
Try this also
in getItemViewType you can set your condition where do you want to show which type of view.
use "if(position==0)" inside getView method
You can check the condition and inflate the layout that you want.
Here is the example. I have three category depending on it I inflate different layouts.
public class ApplicationAdapter extends BaseAdapter
{
private Context cntx;
/**
* 0 - featured 1- Top rated 2- other
*/
private int whichCategory;
/**
* Constructor
*
* #param context
* #param iwhichCategory
* 0 - featured 1- Top rated 2- other
*/
public ApplicationAdapter(Context context, int iwhichCategory)
{
cntx = context;
whichCategory = iwhichCategory;
}
#Override
public int getCount()
{
return entApplications.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (whichCategory == 0)
convertView = inflater.inflate(R.layout.featured_app_grid_item, parent, false);
if (whichCategory == 1)
convertView = inflater.inflate(R.layout.other_app_grid_item, parent, false);
if (whichCategory == 2)
convertView = inflater.inflate(R.layout.other_app_grid_item1, parent, false);
}
}
}

Categories

Resources