2017-08-07 4 views
-1

Innerhalb einer CardView Ich habe eine LinearLayout mit drei TextViews drin und ich möchte den Text von diesen TextViews bekommen. Meine XML-Datei sieht wie folgt aus:Wie bekomme ich Text von TextView in einem CardView?

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

Dann möchte ich Iterierte durch alle CardViews in der übergeordneten und die Linearlayout Textviews bekommen. Etwas wie folgt aus:

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

Aber ich bin nicht in der Lage die Methoden getText().toString(); so zu nennen. Wie kann ich den Text von diesen TextViews innerhalb des CardViews erhalten?

Antwort

0

So viele Optionen hier.
1. Give-ID für Ihre Textviews
2. Verwenden Sie for in Linearlayout mit id innerLayout
3. Wenn Ihre Benutzeroberfläche ist dynamisch, und Sie müssen Textviews von der Verteidigung

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

Thank you! Es funktioniert perfekt, besonders wenn ich mehr als ein CardView habe. – Sumsar9000

+0

Kein Problem, Mann. – Fr099y

0

geben Sie einfach den Verweis auf Textview

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

Es ist nicht funktioniert, weil die Textview bekommen eine andere ist innerhalb Viewgroup welches @ + id/innerLayout ..

ich für Sie eine einfache Funktion machen ..

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

und Sie können es wie folgt verwenden ..

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

hoffen, dass es Ihnen helfen kann .. :)

Verwandte Themen