我正在为Android应用程序字谜
i am working on CrossWord application for Android.
我有一个GridView
I have a gridview
public class MainActivity extends Activity {
private GridView gridView;
private Button button;
private ArrayAdapter<String> adapter;
static final String[] numbers = new String[] { "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
button = (Button) findViewById(R.id.buttonSubmit);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
final StringBuilder sb = new StringBuilder();
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
TextView tv = (TextView) v;
tv.setBackgroundColor(Color.GREEN);
tv.setTag("1");
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
}
public void onSubmitClick(View v){
for(int i=0;i<adapter.getCount();i++){
String s = adapter.getItem(i);
}
}
XML文件:
xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/buttonSubmit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Submit"
android:onClick="onSubmitClick">
</Button>
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/buttonSubmit"
android:layout_alignParentTop="true"
android:columnWidth="30dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
我要的是遍历GridView和发现textviews与标签1 ...但adapter.getItem只返回字符串...任何帮助?
What i want is to iterate through gridview and find textviews with tag1 ... but adapter.getItem returns only string ... any help ?
您可以遍历一个GridView(或任何其他的ViewGroup)的所有儿童提供 getChildAt()
:
You can iterate over all children of a GridView(or any other ViewGroup) with getChildAt()
:
for(int i=0; i<myGridView.getChildCount(); i++) {
TextView child = (TextView)mGridView.getChildAt(i);
// do stuff with child view
}
请注意,有时的GridView
的孩子是另一个的ViewGroup
,所以你可以通过是的孩子,等等。
Notice that sometimes a GridView
's child is another ViewGroup
, so you may need to loop through it's children, etc.
不过...一个更好的方式来做到这一点可能是保持一个单独的列表的标签元素
。你可以简单地将它们添加到列表中标记的时候,并遍历整个列表。
However... a better way to do this might be to keep a separate List
of "tagged" elements. You can simply add them to the list when tagged and iterate over the entire list.