Android textview 适配器没有返回带有标签的特定 textview 的 id

Android textview adapter is not returning id of particular textview find with tag

我正在学习 android 开发并开发一个小游戏应用程序。所以这是我的有效代码,

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View gridViewContent = layoutInflater.inflate(R.layout.game_grid_view_content, null);

    final TextView tv = (TextView) gridViewContent.findViewById(R.id.g_v_content);
    tv.setHeight((gridViewHeight/8)+5);
    tv.setTag(position + 501);
    tv.setId(position + 1001);
    tv.setText(String.valueOf(
            ((TextView)gridViewContent.findViewWithTag(tv.getTag())).getId()
    ));

所以这正在工作并将文本设置为所有文本视图,如 1001、1002、1003 等。但是当我试图通过更改

获取特定文本视图的 ID 时
((TextView)gridViewContent.findViewWithTag(501)).getId();

这是我的第一个 TextView,所以不是将文本 1001 设置到所有电视,应用程序崩溃 NullPointerException。那么谁能告诉我如何通过 id 或标签或任何东西来定位特定的文本视图?我使用 501 只是为了举例,因为我将使用 int 变量来存储以前点击的电视的 id 或标签。我试图通过 id 查找视图,但它也不起作用。

感谢您的宝贵时间。请记住,我在 android 还是新手,正在学习并寻求帮助以找出我错的地方。

这是我的文本视图适配器

@Override
    public View getView(final int position, final View contextView, ViewGroup parent)
    {

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View gridViewContent = layoutInflater.inflate(R.layout.game_grid_view_content, null);

            final TextView tv = (TextView) gridViewContent.findViewById(R.id.g_v_content);
            tv.setHeight((gridViewHeight / 8) + 5);
            tv.setTag(position + 501);
            tv.setId(position + 1001);

        TextView textView = null;
        ViewGroup row = (ViewGroup) parent.getParent();
        for (int itemPos = 0; itemPos < row.getChildCount(); itemPos++) {
            View view = row.getChildAt(itemPos);
            if (view instanceof TextView) { //do a getTag here and get the textView you need
                textView = (TextView) view; //Found it!
                break;
            }
        }

        tv.setText(String.valueOf(
                textView.getId()
        ));
}

这是我的布局xml文件代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    tools:context="io.github.viralj.matchfun.PlayGameActivity"
    android:background="#000000">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play_game_current_score"
        android:textSize="20sp"
        android:id="@+id/current_score"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#ecf0f1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play_game_high_score"
        android:id="@+id/high_score"
        android:textSize="20sp"
        android:layout_alignTop="@+id/current_score"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#ecf0f1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/high_score"
        android:background="#bdc3c7"
        android:layout_marginTop="5dp"/>

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/playGameGridTextView"
        android:layout_marginTop="50dp"
        android:horizontalSpacing="1sp"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1sp"
        android:gravity="center_horizontal"
        >

        </GridView>


</RelativeLayout>

我相信您是……弄错了这两个标签。

setTag() 可以将任何对象附加到视图,而 XML 标签只是一个字符串 - 我相信 findViewByTag 会尝试匹配通过 XML 传递的标签,而不是附加的标签通过代码——因为它可以是任何对象。 编辑

掌握父布局。然后获取该父布局的所有子项。这条线的东西:

 TextView textView = null; 
    ViewGroup row = (ViewGroup) v.getParent();
    for (int itemPos = 0; itemPos < row.getChildCount(); itemPos++) {
        View view = row.getChildAt(itemPos);
 if (view instanceof TextView) { //do a getTag here and get the textView you need
             textView = (TextView) view; //Found it!
             break;
        }
    }