【发布时间】: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