3

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
tshepang
  • 12,111
  • 21
  • 91
  • 136
Bardya Momeni
  • 337
  • 4
  • 13
  • Please provide the source code of `myAdapter`. – Wenhui Nov 30 '12 at 01:04
  • waht do you by `grayed out` ? – Mohsin Naeem Nov 30 '12 at 02:23
  • it's interesting that when I change the context function in this line: myAdapter adapter = new myAdapter(**getApplicationContext()**, menu.all_array) to **getBaseContext()** the grayed outing of the listview item's are solved... can anyone explain please? – Bardya Momeni Nov 30 '12 at 02:31

1 Answers1

3

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

Ben
  • 51,770
  • 36
  • 127
  • 149
BurgerZ
  • 232
  • 4
  • 5
  • I just had same issue, replaced `getApplicationContext()` by `getBaseContext()`, and it worked. But can you be a bit more specyfic why to use one or another, and why with the first one the list is greyed out? – unmultimedio May 29 '14 at 17:18
  • Using `getBaseContext` can be null in case of it's activity was destroyed, so using ActivityName.this (Context). See this [answer](http://stackoverflow.com/a/15875817/1657823) for explanation of contexts. – BurgerZ Jun 02 '14 at 14:47