【问题标题】:Android textview adapter is not returning id of particular textview find with tagAndroid textview 适配器没有返回带有标签的特定 textview 的 id
【发布时间】:2015-05-14 22:02:59
【问题描述】:

我正在学习 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 或 tag 或任何东西来定位特定的文本视图?我只是以 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>

【问题讨论】:

    标签: java android textview adapter android-adapter


    【解决方案1】:

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

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

    【讨论】:

    • 好的,所以我尝试并使用了 findViewById 并传递了 id 1001,但这也不起作用。那么有什么建议,如何获取特定文本视图的数据?
    • 原因是它在 0 处获取文本视图,而在网格视图之前我有两个文本视图。
    • 没关系。在 ViewGroup row = (ViewGroup) v.getParent(); 中,v 是你的 gridViewContent。
    • 好的。让我们一步一步来。注释这段代码 TextView textView = null; ViewGroup row = (ViewGroup) parent.getParent(); for (int itemPos = 0; itemPos
    • 然后替换 tv.setText(String.valueOf( textView.getId() )); with tv.setText(String.valueOf( textView.getTag() ));
    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多