2017-08-07 13 views
-1

CardViewの中にLinearLayoutと3つのTextViewsがあり、これらのTextViewsからテキストを取得したいと考えています。それから私は、親のLinearLayout内のすべてのCardViewsを通じて反復にしたいCardView内でTextViewからテキストを取得するには?

<LinearLayout 
     android:id="@+id/defense" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     > 

     <android.support.v7.widget.CardView 
      android:id="@+id/cardCB" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:layout_weight="1" 
      app:cardBackgroundColor="@android:color/transparent" 
      app:cardElevation="0dp"> 

      <LinearLayout 
       android:id="@+id/innerLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="Rannochia" 
        android:textAlignment="center" 
        android:textStyle="bold"/> 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="78" 
        android:textAlignment="center" 
        android:textStyle="bold"/> 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="CB" 
        android:textAlignment="center" 
        android:textStyle="bold"/> 

      </LinearLayout> 
     </android.support.v7.widget.CardView> 
     </LinearLayout> 

とTextViewsを取得:私のXMLファイルは、次のようになります。このような何か:

 for (int i = 0; i < defense.getChildCount(); i++){ 
     String getName = ((CardView)defense.getChildAt(i)).getText().toString(); 
     id[i] = getName; 
    } 

しかし、私はこのようなメソッドgetText().toString();を呼び出すことができませんよ。 CardView内でこれらのTextViewからテキストを取得するにはどうすればよいですか?

答えて

0

を参照するに、ここで非常に多くのオプションを与えます。
1. ID innerLayout
3を使用してTextViewsため与えるのIDのLinearLayoutで
2. forあなたのUIは動的であり、あなたは防衛

for (int i = 0; i < defense.getChildCount(); i++){ 
     CardView card = defense.getChildAt(i); 
     ViewGroup viewGroup = ((ViewGroup)card.getChildAt(0)); 
     for(int j=0;j<viewGroup.getChildCount();j++){ 
      String getName = ((TextView)viewGroup.getChildAt(j)).getText().toString(); 
      id[i] = getName; 
     } 
    } 
+0

ありがとうございます!私は複数のCardViewを持っているとき、完璧に、espacially動作します。 – Sumsar9000

+0

問題はありません。 – Fr099y

0

だけのTextView

for (int i = 0; i < defense.getChildCount(); i++){ 
     String getName = ((TextView)defense.getChildAt(i)).getText().toString(); 
     id[i] = getName; 
    } 
0

からTextViewsを取得する必要がありますこれは、TextViewのために動作しない場合には

private void loopViews(ViewGroup view) { 
for (int i = 0; i < view.getChildCount(); i++) { 
    View v = view.getChildAt(i); 

    if (v instanceof EditText) { 
     // will be executed when its edittext 
     Log.d("Check", "This is EditText"); 

    } else if (v instanceof TextView) { 
     // will be executed when its textview,, and get the text.. 
     TextView x = (TextView) v; 
     String aa = x.getText().toString(); 
     Log.d("Check", "This is TextView with text : " +aa); 

    } else if (v instanceof ViewGroup) { 

     // will be executed when its viewgroup,, and loop it for get the child view.. 
     Log.d("Check", "This is ViewGroup"); 
     this.loopViews((ViewGroup) v); 
    } 
} 

..私はあなたのための簡単な関数を作る+ ID/innerLayout @ある別のViewGroup ..

の内側にあります

とあなたはこのようにそれを使用することができます。..

LinearLayout def = (LinearLayout) findViewById(R.id.defense); 
    loopViews(def); 

はそれが..あなたを助けることができると思います:)

関連する問題