0

I am new to Android Application in which i need a help my checkbox which is been placed in gridview is not getting visible when i click the icon which is been placed in Toolbar

Code:-

public class MyActivity extends Fragment {
   public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
     myView=inflater.inflate(R.layout.activity_room_summary,null);
     setHasOptionsMenu(true);
     initializeValue();
     return myView;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == R.id.action_dash){
        checkBox.setVisibility(View.VISIBLE);//Not working
        return true;
    }
      return super.onOptionsItemSelected(item);
    }
}

public void initializeValue(){
  adapter = new MyAdapter(ctx, R.layout.activity_list, listMy);
}


public class MyAdapter extends ArrayAdapter<My> {
  @Override
  public View getView ( int position, View convertView, ViewGroup parent ) {
    checkBox.setVisibility(View.INVISIBLE);
  }
}
Atul Dhanuka
  • 1,453
  • 5
  • 20
  • 56

2 Answers2

1

you also need to refresh your adapter:

adapter.notifyDataSetChanged();
sj_8
  • 173
  • 1
  • 16
1

It will not work like that, because the CheckBox is in GridView so you need to notify the adapter.

Try this:

public class MyActivity extends Fragment {

   boolean mIsCheckBoxVisible;

   public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
     myView=inflater.inflate(R.layout.activity_room_summary,null);
     setHasOptionsMenu(true);
     initializeValue();
     return myView;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == R.id.action_dash){
        mIsCheckBoxVisible = true;
        adapter.notifyDataSetChanged();
        return true;
    }
      return super.onOptionsItemSelected(item);
    }
}

public void initializeValue(){
  adapter = new MyAdapter(ctx, R.layout.activity_list, listMy);
}


public class MyAdapter extends ArrayAdapter<My> {
  @Override
  public View getView ( int position, View convertView, ViewGroup parent ) {

     if(mIsCheckBoxVisible){
       checkBox.setVisibility(View.VISIBLE);
     } else {
       checkBox.setVisibility(View.INVISIBLE);
     }
  }
}
Rami
  • 7,879
  • 12
  • 36
  • 66
  • just want to ask how i can know which checkbox is been checked and what is the textview value of that checked one – Atul Dhanuka Oct 13 '15 at 05:39
  • i tried that example i didnt get the answer plz help me out if possible using above example – Atul Dhanuka Oct 16 '15 at 04:36
  • This should work, did you call notifyDataSetChanged(); after updating the boolean? otherwise i need to see your code, to know the problem. – Rami Oct 16 '15 at 08:17