how to access to a specific element of every grid view item
Asked Answered
U

2

11

I have a gridview in which there are two elements in each of its items, the first is an image and the second is a title, the title is invisible in the launching of the application, but I have a button outside the grid view that when I click on it, I want to change the visibility of the titles of items to become visible, my problem is that I can not access each title of each item in the grid view. When I set the visibility of the titles (TextView) in the onClick method of the independent button in my activity, it changes the visibility ONLY for the FIRST item in the grid view!

This sketch represent my interface, so the titles are invisible in the beginning, but when i click on "SetVisibilityButton", i want to set them to Visible :

    Image1    Image2      Image3
    Title1    Title2      Title3

    Image4    Image5      Image6
    Title4    Title5      Title6

    Image7    Image8      Image9
    Title7    Title8      Title9


    ----------------------------
    SetVisibilityButton
    ____________________________        

I set the grid view in my oncreate() activity :

    favorGrid = (GridView) findViewById(R.id.favorGrid);
    favorGrid.setAdapter(adapter);

In my ImageAdapter class, this is my getView() method :

          @Override
       public View getView(int position, View convertView, ViewGroup parent) 
       {
          View MyView = convertView;

           /*we define the view that will display on the grid*/

             //Inflate the layout
             LayoutInflater li = getLayoutInflater();
             MyView = li.inflate(R.layout.favor_item, null);

             // Add The Text!!!
             TextView tv = (TextView)MyView.findViewById(R.id.title);
             tv.setText(mTitles[position]);

             // Add The Image!!!           
             ImageView iv = (ImageView)MyView.findViewById(R.id.favor_item_image);
             iv.setImageResource(mThumbIds[position]);
          return MyView;
       }

To get the title textview from my main activity and set his visibility, I tried :

    TextView title = (TextView) findViewById(R.id.title);
    title.setVisibility(View.VISIBLE);

and tried :

     // gv is the gridview (R.id.gridview)
     TextView title = (TextView)gv.findViewById(R.id.title);
     title.setVisibility(View.VISIBLE);

and tried :

      LinearLayout layout = (LinearLayout) findViewById(R.id.gridLayout);
      TextView title = (TextView)layout.findViewById(R.id.title);
      title.setVisibility(View.VISIBLE);

But all these solutions set the visibility for only the first element in the grid view.

I spent a lot of time with this problem but I don't yet found a solution, does anyone could help me to fix it please thank you in advance

Unseam answered 20/1, 2012 at 16:7 Comment(0)
F
28

This is simple:

GridView mGridView (Your object instance)

final int size = mGridView.getChildCount();
for(int i = 0; i < size; i++) {
  ViewGroup gridChild = (ViewGroup) mGridView.getChildAt(i);
  int childSize = gridChild.getChildCount();
  for(int k = 0; k < childSize; k++) {
    if( gridChild.getChildAt(k) instanceof TextView ) {
      gridChild.getChildAt(k).setVisibility(View.GONE);
    }
  }
}
Free answered 20/1, 2012 at 16:32 Comment(6)
Thanks for ur reply,i didn't understand the first line of your code but i tried with this code, but it did nothing :Unseam
favorGrid = (GridView) findViewById(R.id.favorGrid); favorGrid.setAdapter(adapter); final int size = favorGrid.getChildCount(); for(int i = 0; i < size; i++) { ViewGroup gridChild = (ViewGroup) favorGrid.getChildAt(i); int childSize = gridChild.getChildCount(); for(int k = 0; k < childSize; k++) { if( gridChild.getChildAt(k) instanceof TextView ) { gridChild.getChildAt(k).setVisibility(View.VISIBLE); } } }Unseam
Great ! it works ! Thank you very much, but i have another question: if i have two textview in every item of the grid view, how can i precise witch textview i want to update ? because here : "gridChild.getChildAt(k) instanceof TextView" you identify only the type of the view (TextView), and not a specific TextView (if we have more than only one)Unseam
use on that child "getId()", you can compare the id with R.id.YOUR_SPECIFIED_ID to determine if its the view you search for :)Free
@Alaoui Ghita. If you think that answer helped you you should accept it. that way it will encourage ppl to answer your questions in future.Subjacent
Inside public View getView(final int position, View convertView, ViewGroup parent) you can use parent to access different views since in this case GridView mGridView would be the same as parent. This approach can be used if you were to do different stuff with different views. For example in my case, I had to measure the height of each row and then set every row height to a specific value to fit correctly.Assegai
U
4

The right way to get the view of the listView for a specific position is the next one:

View viewItem = list.getChildAt(position);
if (viewItem != null) {                 
   TextView text = (TextView) viewItem.findViewById(R.id.item);
}
Uttasta answered 26/9, 2014 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.