Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview's items grayed out after setting the listview's adapter in a button's onclicklistener...?

I have a issue in my app; in my app, I refine listviews adapter and it's items using buttons, but when I click a button like "Show all records", the items in the listview are grayed out.

I think the code speaks for itself:

show_all_Button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Rest_menu menu = new Rest_menu();
                menu = (Rest_menu)v.getTag();
                myAdapter adapter = new myAdapter(getApplicationContext(), menu.all_array);

                listView.setAdapter(adapter);

            }
        });

as you can see, by clicking show_all_button, the listview's adapter sets to show all items, and the items are shown, but rows are grayed out. what can I do to make them Not grayed out? EDIT: after some coding, edited the adapter, and here's the code of the listview's adapter:

public class foodAdapter extends ArrayAdapter<Food> {


    Context context;
    public Food[] iteminarow;
    public foodAdapter(Context context,Food[] iteminarow)
    {
        super(context, R.layout.listitem,iteminarow);
        this.context=context;
        this.iteminarow=iteminarow;
    }

    static class ViewHolder{
        TextView foodname;
        TextView foodprice;
        TextView foodinfo;
        public ViewHolder(TextView foodname,TextView foodprice,TextView foodinfo)
        {
            this.foodname=foodname;
            this.foodprice=foodprice;
            this.foodinfo=foodinfo;
        }
    }//end of ViewHolder

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView name,price,info;
        Food f = (Food)this.getItem(position);

        if(convertView==null)
        {
            LayoutInflater LI = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = LI.inflate(R.layout.listitem,parent,false);
            name=(TextView)convertView.findViewById(R.id.foodname);
            price=(TextView)convertView.findViewById(R.id.foodprice);
            info=(TextView)convertView.findViewById(R.id.foodinfo);
            convertView.setTag(new ViewHolder(name, price, info));
        }//end of if



        else {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        name = holder.foodname;
        price = holder.foodprice;
        info = holder.foodinfo;
        }//end of else

        name.setText(f.getName().toString());
        price.setText(f.getPriceString());
        info.setText(f.getInfo().toString());

        return convertView;
    }


}//end of adapter
like image 821
Bardya Momeni Avatar asked Dec 01 '25 15:12

Bardya Momeni


1 Answers

You don't have to use getApplicationContext() in Activities. Use this or YourActivity.this.

  • getBaseContext() - gets you context for your current activity (but you should use Context instead) and can be destroyed when activity is destroyed

  • getApplicationContext()` - app global context which is the same for all activities through the life cycle.

If you want to set an adapter for your view, use your activity's context like YourActivity.this (baseContext does the trick, but Google does not recommend using it).

like image 142
BurgerZ Avatar answered Dec 04 '25 05:12

BurgerZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!