Listview Scrolling changes Button Visibility? - android

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;
} }

Related

How to show/hide a layout when gridView item is selected/unselected in Android using Custom BaseAdapter

this is my Adapter class
public class GridViewCustomAdapter extends BaseAdapter {
private List<ProductParameterBO> availList;
private LayoutInflater inflater;
Context context;
public GridViewCustomAdapter(Context ctx,List<ProductParameterBO> list){
this.context = ctx;
this.availList = list;
}
#Override
public int getCount() {
return availList.size();
}
#Override
public Object getItem(int position) {
return availList.get(position);
}
#Override
public long getItemId(int position) {
ProductParameterBO c = availList.get(position);
// long id = c.getTimeId();
return 0;
}
#Override
public View getView(final int position,View convertView,final ViewGroup parent) {
View row = convertView;
final TeeTimeHolder holder;
if (row == null){
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_row,parent,false);
holder = new TeeTimeHolder();
holder.myImage =(ImageView)row.findViewById(R.id.imageView10);
holder.imageflip =(ImageView)row.findViewById(R.id.img_flp);
holder.name =(TextView)row.findViewById(R.id.textView47);
holder.edit =(TextView)row.findViewById(R.id.textView49);
holder.rl =(RelativeLayout)row.findViewById(R.id.grid_img_ovrly);
// holder.row =(RelativeLayout)row.findViewById(R.id.row);
row.setTag(holder);
}
else {
holder =(TeeTimeHolder)row.getTag();
}
holder.name.setText(availList.get(position).getParameterName());
holder.myImage.setImageResource(R.drawable.toi);
holder.rl.setVisibility(View.GONE);
return row;
}
static class TeeTimeHolder {
ImageView myImage,imageflip;
TextView name,edit;
RelativeLayout rl;
}
}
i just want to show Relativelayout r1 when row is selected and when i select the next row ,previous row must hide Relativelayout r1 layout and show on row which is selected.. Now in my case when i select the next row ,then its not hiding from previous row ..layout still showing on all row which i select.
Please help me to sort out this problem..any help would be appreciated in advanced..
int priviousPosition=0;
ArrayList<View> viewList= new ArrayList<View>();
holder.rl.setTag(position);
viewList.add(holder.rl);
row.findViewById(R.id.grid_img_ovrly).setOnClickListener(new OnClickListener() {
public void onClick(View v1) {
View v=viewList.get(priviousPosition);
v.setVisibility(View.GONE);
int postition=(Integer) v1.getTag();
priviousPosition=postition;
notifyDataSetChanged();
}
});

My images changing during scroll in listview android

In last week, i just try to resolve this problem. see every similar questions and i don't know why, solutions don't work for me.
I have a list view and each item has an image view. when clicked on image view , i want to change image resource. well. all done. but when scrolling, image resources change.
my full code is:
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<String> items;
public CustomBaseAdapter(Context context,ArrayList<String> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.hashCode();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
}
});
return vi;
}
}
where i have mistake?
you have to use one boolean value in holder to check whether your background image is set or not for particular row.
holder
private class ViewHolder{
TextView titr;
ImageView image;
boolean isSet;
}
set image
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
holder.isSet = true
}
});
if(holder.isSet)
{
holder.image.setBackgroundResource(R.drawable.fav_ic);
}
else{
holder.image.setBackgroundResource(no image or other image);
}
hope this will help.
try this,
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<Items> items;
public CustomBaseAdapter(Context context,ArrayList<Items> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Items getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.hashCode();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position).getStrTxt());
holder.image.setImageResource(items.get(position).getDrawableImage());
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
items.set(position,new Items(R.drawable.fav_ic));
notifyDataSetChanged();
}
});
return vi;
}
}
Add Model Class :
public class Items {
int drawableImage;
String strTxt;
public Items(int drawableImage) {
this.drawableImage = drawableImage;
}
public int getDrawableImage() {
return drawableImage;
}
public void setDrawableImage(int drawableImage) {
this.drawableImage = drawableImage;
}
public String getStrTxt() {
return strTxt;
}
public void setStrTxt(String strTxt) {
this.strTxt = strTxt;
}
}
This is because when you scroll listview android system always create new row and destroy non-visible rows. You need to modify the getView() as below then everything will be fine :
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position));
if(sp.getBoolean("position="+position, false)){
v.setBackgroundResource(R.drawable.fav_ic);
}
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean("position="+position, true);
edit.commit();
}
});
return vi;
}
You should define layoutInflater in the constructor because getView() call for each row so if you define layoutInflater in getView() then LayoutInflater define again and again for each row.
Yes, this problem appears always in ListView because android always destroy and recreate rows in it.
You have two choices:
First, Modify your Adapter to save status for each row in the ListView and check this status before displaying the row.
Second one, I recommended this one and use it in my code, you can use RecyclerView, this problem will not happen with it.

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.

Listview items background

How to change the background image for list items, i am able to change only 1 item background at a time.
If there are 6 items on the list and if click on 3 items those 3 items background images should be changed, how it is possible
Below is my code
public class Places extends Activity {
private ListView listView;
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_custom_list_view);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("five");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
} else {
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
}
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Try this,it should work logically.(I didn't try it,btw! :P)
...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
EfficientAdapter.saveState.put(position,"selected");
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
...
private class EfficientAdapter extends BaseAdapter {
public static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
for(int i=0;i<5;i++)
{
saveState.put(i,"unselected");
}
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(saveState.get(position).equals("selected"))
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
else
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Every time you click on listview item whole listview is getting refreshed.
So if you want to show previously selected items also then need to keep record of all the item selected. and when listview refreshed you need to check that this positions are previously selected or not according to that set your color.
Hope this help you
try this
android:background="#drawable/img_list_background_repeater"
if(clickWord.size()!=0)
{
for(int i = 0;i<clickWord.size();i++){
if(clickWord.get(i).equalsIgnoreCase(adListText[position])&&clickIndex.get(i)==position){
wordChk.setBackgroundResource(R.drawable.star_1);
}
}
}
Here clickWord is an arraylist of items selected. so when items get selected it will be added in this arraylist and when arraylist is refreshed i check all this using this loop.

Categories

Resources