0

This may be simple but i couldn't find solution for this. The problem is setting the background color for gridview items inside the onitemlongclicklistener method.

I have two linear layouts, one contains the textview and gridview and another contains some buttons(I set this layout like the menubar on top of the screen). I hide the second one from the screen using setVisibility(View.GONE) and when ever I do itemlongclick on the gridview item the second one will appear using setVisibility(View.VISIBLE) at the same time I want to change the bgcolor of the clicked/selected item.

Everything is working fine but whenever the second layout is visible and also whenever scrolling the gridview, the clicked/selected bgcolor is disappear.

I tried so many ways but I couldn't solve this problem. Please have look on my coding and tell me what I did wrong.

gv.setOnItemLongClickListener(new OnItemLongClickListener() {
          public boolean onItemLongClick(AdapterView<?> parent, View strings,
                          int position, long id) {
              final Option o = dir.get(position);
             //gv.performItemClick(gv, position,gv.getItemIdAtPosition(position));

              /* if(pos!=-1) 
              {
              gv.getChildAt(pos).setBackgroundColor(0xffffffff);

              }*/
              strings.setSelected(true);
             // TextView tx=(TextView)strings.findViewById(R.id.grid_item_label);
            //  tx.findViewById(id).setBackgroundColor(0xff0000ff);
             /*if(!o.getData().equalsIgnoreCase("o")){


                 //gv.getChildAt(position).setSelected(true);    
                 fill(new File(new File(o.getPath()).getParent()),position);

          }*/
             /*try
             {
                 gv.getSelectedView().setBackgroundColor(0xffcccccc);
             }
             catch(Exception e){
                Toast.makeText(FffsdActivity.this, "ok\n"+e,Toast.LENGTH_SHORT).show();

             }*/

             /*for(int i=0;i<nofifo;i++)

              {
                 try{
                 if(gv.getChildAt(i).isSelected())
                 {
                       gv.getChildAt(i).setBackgroundColor(0xffbbbbbb); 
                        // Toast.makeText(FffsdActivity.this, "yes ok\n"+i,Toast.LENGTH_SHORT).show();

                 }
                 else{
                           gv.getChildAt(i).setBackgroundColor(0xffffffff); 
                         //Toast.makeText(FffsdActivity.this, "no ok\n"+i,Toast.LENGTH_SHORT).show();

                 }
                 }
                 catch(Exception e)
                 {
                Toast.makeText(FffsdActivity.this, "i wanna e :"+e+"\n"+i,Toast.LENGTH_SHORT).show();

                 }
                try{
                 gv.getChildAt(i).setSelected(false);
                   //Toast.makeText(FffsdActivity.this, "gv ok\n"+i,Toast.LENGTH_SHORT).show();

                }catch(Exception e){
                      Toast.makeText(FffsdActivity.this, "i  wanna setselected e :"+e+"\n"+i,Toast.LENGTH_SHORT).show();

                }

              }*/


             // Toast.makeText(FffsdActivity.this, "i wanna c :"+gv.getChildAt(position).isSelected(),Toast.LENGTH_LONG).show();
             //gv.getSelectedView().setBackgroundColor(0xffcccccc);         
             pos=position;
            if(o.getData().equalsIgnoreCase("parent directory"))
            {
                fill(new File(o.getPath()),-1);
            }
             else if(o.getData().equalsIgnoreCase("folder"))
            {
                 llfftools.setVisibility(View.VISIBLE);
                 TranslateAnimation slide = new TranslateAnimation(0, 0,-llfftools.getHeight(),0 );   
                 slide.setDuration(100);   
                 slide.setFillAfter(true);   
                llfftools.startAnimation(slide); 
                //fill(new File(new File(o.getPath()).getParent()));

                onFolderClick(o);
                 //gv.getChildAt(position).setBackgroundColor(0xffaaaaaa);


            }

and my baseadapter is :

    public class ImageAdapter extends BaseAdapter {
private Context context;
private final List<Option> mobileValues;



public ImageAdapter(Context context,List<Option> fofivalues) {
    this.context = context;
    this.mobileValues = fofivalues;
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;
    //if (convertView == null) {

        gridView = new View(context);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.mobile, null);



    /*} else {
        gridView = (View) convertView;
    }*/
    // set value into textview

                TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
                //to set the max no.of chararcters in textview
                String iname=(mobileValues.get(position).getName().length()>10)?mobileValues.get(position).getName().substring(0, 9)+"..":mobileValues.get(position).getName();
                if(mobileValues.get(position).getPosition()==position&&mobileValues.get(position).getPosition()!=-1)
                   {
                    textView.setText(iname);
                    textView.setTextColor(0xff0000ff);
                   }
                else{
                textView.setText(iname);
                }

                // set image based on selected text
                final ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
                /*gridView.setOnLongClickListener(new OnLongClickListener() {

                    public boolean onLongClick(View v) {
                        gridView.setBackgroundColor(0xffaaaaaa);
                        // TODO Auto-generated method stub
                        return false;
                    }
                });*/
                String mobile = mobileValues.get(position).getData();
                if(mobile=="o")
                {
                 //do nothing
                    imageView.setImageResource(R.drawable.white);
                }

                else if(mobile=="Parent Directory")
                {
                    imageView.setImageResource(R.drawable.parent);
                }
                else if (mobile=="Folder") 
                {
                    imageView.setImageResource(R.drawable.folderg);
                }
                else{
                    imageView.setImageResource(R.drawable.picicon);
                }



    return gridView;
}

public int getCount() {
    return mobileValues.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

    }

thanks in advance and if you want anything more please let me know...

1 Answers1

0

In your XML set the android:background attribute to an xml like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_btn_sessions_pressed"
    android:state_focused="true"
    android:state_pressed="true" />
<item android:drawable="@drawable/home_btn_sessions_pressed"
    android:state_focused="false"
    android:state_pressed="true" />
<item android:drawable="@drawable/home_btn_sessions_selected" android:state_focused="true" />
<item android:drawable="@drawable/home_btn_sessions_default"
    android:state_focused="false"
    android:state_pressed="false" /> </selector>

Don't need to write Java code here.