我有一个gridview,其中每个项目中有两个元素,第一个是图像,第二个是标题,标题在应用程序启动时是不可见的,但是我在网格视图之外有一个按钮,当我单击它,我想将项目标题的可见性更改为可见,我的问题是我无法访问网格视图中每个项目的每个标题。当我在活动中的独立按钮的 onClick 方法中设置标题(TextView)的可见性时,它只会更改网格视图中第一个项目的可见性!
这个草图代表我的界面,所以标题在开始时是不可见的,但是当我点击“SetVisibilityButton”时,我想将它们设置为 Visible :
Image1 Image2 Image3
Title1 Title2 Title3
Image4 Image5 Image6
Title4 Title5 Title6
Image7 Image8 Image9
Title7 Title8 Title9
----------------------------
SetVisibilityButton
____________________________
我在 oncreate() 活动中设置了网格视图:
favorGrid = (GridView) findViewById(R.id.favorGrid);
favorGrid.setAdapter(adapter);
在我的 ImageAdapter 类中,这是我的 getView() 方法:
@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;
}
为了从我的主要活动中获取标题 textview 并设置他的可见性,我尝试了:
TextView title = (TextView) findViewById(R.id.title);
title.setVisibility(View.VISIBLE);
并尝试:
// gv is the gridview (R.id.gridview)
TextView title = (TextView)gv.findViewById(R.id.title);
title.setVisibility(View.VISIBLE);
并尝试:
LinearLayout layout = (LinearLayout) findViewById(R.id.gridLayout);
TextView title = (TextView)layout.findViewById(R.id.title);
title.setVisibility(View.VISIBLE);
但是所有这些解决方案都只为网格视图中的第一个元素设置了可见性。
我花了很多时间解决这个问题,但我还没有找到解决方案,有没有人可以帮助我解决它,请提前谢谢