遍历 gridview 中的所有对象 [英] iterate through all objects in gridview

查看:27
本文介绍了遍历 gridview 中的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Android 版填字游戏应用程序.

我有一个网格视图

public class MainActivity extends Activity {私有 GridView 网格视图;私人按钮按钮;私有 ArrayAdapter适配器;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" };@覆盖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);适配器 = 新的 ArrayAdapter(this,android.R.layout.simple_list_item_1,数字);gridView.setAdapter(适配器);最终 StringBuilder sb = new StringBuilder();gridView.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View v,int 位置,长 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 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" ><按钮android:id="@+id/buttonSubmit"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"机器人:文本=提交"android:onClick="onSubmitClick"></按钮><网格视图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:numColumns="auto_fit"android:stretchMode="columnWidth" ></GridView></RelativeLayout>

我想要的是遍历 gridview 并找到带有 tag1 的 textviews ......但adapter.getItem 只返回字符串......有什么帮助吗?

解决方案

您可以使用 迭代 GridView(或任何其他 ViewGroup)的所有子级getChildAt():

 for(int i = 0; i 

请注意,有时 GridView 的子级是另一个 ViewGroup,因此您可能需要遍历 它的 子级,等等.


然而……更好的方法可能是保留一个单独的标记"List元素.您可以在标记时简单地将它们添加到列表中并遍历整个列表.

i am working on CrossWord application for Android.

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 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>

What i want is to iterate through gridview and find textviews with tag1 ... but adapter.getItem returns only string ... any help ?

解决方案

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
    }

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.

这篇关于遍历 gridview 中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆