我正在學習Android開發和工作的小遊戲應用程序。因此,這裏是我的代碼工作,Android的textview適配器沒有返回特定的textview查找與標籤
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將文本全部textviews,1002,1003等,但是,當我試圖通過改變
獲得特定的TextView的ID((TextView)gridViewContent.findViewWithTag(501)).getId();
這是我的第一個TextView
,所以不是將文本1001設置爲全部電視,而是應用程序與NullPointerException
一起墜毀。那麼有誰能告訴我如何通過id或標籤或任何東西來定位特定的文本視圖?我剛剛舉例說501,因爲我將使用int變量來存儲以前點擊過的電視的ID或標籤。我試圖通過ID查找視圖,但它也不起作用。
謝謝你的時間。請記住,我在android和學習方面仍然很陌生,希望找到幫助以找到我錯在哪裏。
這裏是我的TextView適配器
@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>
好吧,所以我嘗試和使用findViewById並通過ID 1001也沒有工作。那麼有什麼建議,如何獲取特定文本視圖的數據? –
我編輯了我的答案。請看一看。 –
這不起作用。 –