How to make spinner show full items in Android - 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.

Related

Listview showing previous selection [ANDROID]

In my app i am showing some items in list view, each row have one name and one tick image. Tick image only shows up when item is selected(checked), The selection is working fine but the problem is when a new item is selected tick of previously selected item doesn't goes invisible. How this can be fixed? Any help will be appreciated
here is my code
Adapter
public class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
Context context;
private ArrayList<String> responseList;
private LinearLayout parent_choice_list_container;
private TextView item_name;
private ImageView iv_choice_list_item_selected;
public ListAdapter (Context context, ArrayList<String> responseList) {
this.context = context;
this.responseList = responseList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedIndex(int ind) {
selectedIndex = ind;
notifyDataSetChanged();
}
#Override
public int getCount() {
return responseList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.menu_list_item, null);
}
parent_choice_list_container = (LinearLayout) view.findViewById(R.id.parent_choice_list_container);
item_name = (TextView) view.findViewById(R.id.item_name);
iv_choice_list_item_selected = (ImageView) view.findViewById(R.id.iv_choice_list_item_selected);
iv_logo = (ImageView) view.findViewById(R.id.iv_blog_category_logo);
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
} list_item_name.setText(responseList.get(position).getName());
return view;
}
}
FragmentPart (Item Click Listener)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
response.get(position).setSelected(true);
ListAdapter.notifyDataSetChanged();
}
It sounds like you're using a CheckBox.
Have you tried conditionally updating the Views visibility depending on whether the item isChecked (and not isSelected)?
you should set tag for each image to keep track of its state (0 = not selected/invisible , 1 = selected/visible) , e.g:
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setTag(1);
}
and you can add this :
if (iv_choice_list_item_selected.getTag() == 1) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
}

Listview Scrolling changes Button Visibility?

I am having a listview in my App. Each row has an button. I'm hiding the button for some rows with setVisibility. But the buttons visibility changes after scrolling the list. How can i stop this change ?
I already saw a question with the checkbox with Listview. But i don't know how to implement it for the buttons. So please guide me!
ADAPTER
public class published_adapter extends BaseAdapter {
Context con;
ArrayList<HashMap<String, String>> class_list;
LayoutInflater inflater;
public class ViewHolder
{
TextView title,description,class_section,date;
ImageButton download;
Button viewasgn;
}
public published_adapter(Context co, ArrayList<HashMap<String, String>> list1) {
class_list = list1;
con = co;
inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return class_list.size();
}
#Override
public Object getItem(int arg0) {
return class_list.get(arg0).get("class_name");
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
View row = arg1;
ViewHolder holder = new ViewHolder();
if(row == null)
{
row = inflater.inflate(
R.layout.assignment_adapter_layout, arg2, false);
// initialize the elements
holder.download = (ImageButton) row.findViewById(R.id.download);
holder.title = (TextView) row.findViewById(R.id.title);
holder.description = (TextView) row.findViewById(R.id.description);
holder.class_section = (TextView) row.findViewById(R.id.class_section);
holder.date = (TextView) row.findViewById(R.id.date);
holder.viewasgn = (Button) row.findViewById(R.id.attend);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
String type = class_list.get(arg0).get("ASSIGNMENT_TYPE");
if (class_list.get(arg0).get("TOTAL_SUBMISSION").equals("0")) {
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
}
else{
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.VISIBLE);
holder.viewasgn.setText("VIEW");
}
return row;
}
}
Update your code like this
if (class_list.get(arg0).get("TOTAL_SUBMISSION").equals("0")) {
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.INVISIBLE);
holder.viewasgn.setText("");
}
else{
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.VISIBLE);
holder.viewasgn.setText("VIEW");
}
Either add the setVisibility(View.INVISIBLE) like #Solhail suggested or hide those buttons by default in your assignment_adapter_layout.xml by adding android:visibility="invisible" in your Button's properties, and don't change a thing in your current adapter code.
In your case Views will be recreated when listView is scroll.
To prevent this, you should not use ViewHolder.
You need to user Adapter like this:
(Only if you have not a lot elements)
public class SampleAdapter extends BaseAdapter {
private ArrayList<String> arrayList;
private Activity activity;
public SampleAdapter(ArrayList<String> arrayList, Activity activity) {
this.arrayList = arrayList;
this.activity = activity;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = activity.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.sample_element, parent, false);
TextView textViewTitle = (TextView) view.findViewById(R.id.text_view_title);
textViewTitle.setText(arrayList.get(position));
return view;
} }

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..

ListView items repeat in BaseAdapter

I'm pulling data and loading it into a BaseAdapter for my List View. Currently, the items in the listview are the same as the last item added, so the list looks like this:
Item 3
Item 3
Item 3
Instead of:
Item 1
Item 2
Item 3
I've checked over the data sources, and the data that's loaded from the source is unique each time. I think there's somehting wrong with my BaseAdapter, but I'm not sure what it is. Here's my BaseAdapter:
BaseAdapter.java
ArrayList<ThreadListData> myList = new ArrayList<>();
LayoutInflater inflater;
Context context;
public ThreadBaseAdapter(Context context, ArrayList<ThreadListData> myList) {
this.context = context;
inflater = LayoutInflater.from(this.context);
this.myList = myList;
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ThreadListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(this.context);
convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);
}
ThreadListData listData = getItem(position);
TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
TextView username = (TextView)convertView.findViewById(R.id.username_newthread);
name.setText(listData.getName());
username.setText(listData.getName());
ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);
BitmapTask task = new BitmapTask(listData.getImage());
task.execute(profile);
return convertView;
}
I'll start with my BaseAdapter, and can show more code if requested. All Help Is Appreciated!
You missed to return the position
Modifiy your getItemId as
#Override
public long getItemId(int position) {
return position;
}
There are 2 things you need to change:
1. You return wrong item position
#Override
public long getItemId(int position) {
return 0;
}
It should be
#Override
public long getItemId(int position) {
return position;
}
2. Your declared a LayoutInflater and create new instance in constructor. But in the getView(), you create another LayoutInflater. Look at updated code below
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// LayoutInflater inflater = LayoutInflater.from(this.context);
inflater = LayoutInflater.from(this.context);
convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);
}
ThreadListData listData = getItem(position);
TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
TextView username = (TextView)convertView.findViewById(R.id.username_newthread);
name.setText(listData.getName());
username.setText(listData.getName());
ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);
BitmapTask task = new BitmapTask(listData.getImage());
task.execute(profile);
return convertView;
}

GridView's first Item is not showing up

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.

Categories

Resources