2017-08-05 20 views
0
public void filter(TableLayout tl, String regex) { 
     TableRow tr; 
     TextView tv; 
     Pattern p; 
     Matcher m; 

     p = Pattern.compile(regex); 
     int n = tl.getChildCount(); 
     for (int i = 0; i < n; i++) { 
      tr = (TableRow) tl.getChildAt(i); 
      tv = (TextView) tr.getChildAt(0); 
      m = p.matcher(tv.getText()); 
      if (m.find()) { 
       tr.setVisibility(View.VISIBLE); 
      } else { 
       tr.setVisibility(View.GONE); 
      } 

私はaftertextchangedでテーブルレイアウトのフィルタを実行しようとしました。テキストの値はフィルタメソッドに完全に渡されますが、tv = (TextView) tr.getChildAt(0);が私に与えます。android.widget.LinearLayoutはandroid.widget.TextViewにキャストできません。ほとんどの他のフォーラムでは、ハードコードされているので、textviewのidがあることを示しているので、動的に配置されたtextviewのエラーを解決するものは見つけられませんでした。以下は、うまく動作するテーブルレイアウトに追加する例です。テーブル外のテキストビューをテーブル行のテキストビューと比較するにはどうすればよいですか?

public void addData(ArrayList<Transactions> Transactions) { 

     tl.removeAllViewsInLayout(); 
     addHeader(); 
     int j = 1; 
     for (Iterator i = Transactions.iterator(); i.hasNext(); j++) { 
      Transactions p = (Transactions) i.next(); 

      if (p.getAdminNo().equals(adminNo)) { 

       /** Create a TableRow dynamically **/ 
       tr = new TableRow(this); 

       /** Creating a TextView to add to the row **/ 
       label = new TextView(this); 
       label.setText(String.valueOf(j)); 
       label.setId(p.getId()); 

       acd = String.valueOf(p.getId()); 
       label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
         TableRow.LayoutParams.WRAP_CONTENT)); 
       label.setPadding(5, 5, 5, 5); 
       label.setBackgroundColor(Color.GRAY); 
       LinearLayout Ll = new LinearLayout(this); 
       LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
         TableRow.LayoutParams.WRAP_CONTENT); 
       params.setMargins(5, 2, 2, 2); 
       //Ll.setPadding(10, 5, 5, 5); 
       Ll.addView(label, params); 
       tr.addView((View) Ll); // Adding textView to tablerow. 

これは私のxmlファイルです。 edittextに値を入れると、フィルタメソッドが起動します。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <EditText 
     android:layout_width="134dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/search" 
     /> 

    <Spinner 
     android:id="@+id/spinner" 
     android:layout_width="244dp" 
     android:layout_height="44dp" 
     android:textColor="#00f0ff" 
     /> 

    <ScrollView 
     android:id="@+id/layout" 
     android:layout_height="match_parent" 
     android:scrollbars="horizontal|vertical" 
     android:layout_width="match_parent" 
     android:layout_marginTop="5dip" 
     android:scrollbarStyle="outsideInset" 
     android:fillViewport="true"> 

     <HorizontalScrollView 
      android:id="@+id/horizontalView" 
      android:layout_height="wrap_content" 
      android:scrollbars="horizontal|vertical" 
      android:layout_width="wrap_content" 
      android:layout_marginTop="5dip"> 

      <TableLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/maintable" > 
      </TableLayout> 
     </HorizontalScrollView> 
    </ScrollView> 
</LinearLayout> 
+0

実際に何が起こっているのかわかるようにxmlファイルに投稿する必要がありますが、そのキャストの例外以降、期待通りの順番でビューが返されない可能性があります。親切にxmlファイルを投稿してください – shegzy

+0

私はそれに応じて追加しました。もう一度見ていただけますか?必要に応じて、私はあなたが私を助けるために必要な他のコードを掲載します。 –

答えて

0

あなたがテーブルビューに追加する最初の項目は、あなたがのTableRowに追加するそのわずか項目以来のLinearLayout

Ll.addView(label, params); //adding textview to linearlayout 
tr.addView((View) Ll); // Adding linearlayout to tablerow. 

であるため、その投げキャスト例外がある理由は、私があなたは直線レイアウトが必要ではないと思います。

public void addData(ArrayList<Transactions> Transactions) { 

    tl.removeAllViewsInLayout(); 
    addHeader(); 
    int j = 1; 
    for (Iterator i = Transactions.iterator(); i.hasNext(); j++) { 
     Transactions p = (Transactions) i.next(); 

     if (p.getAdminNo().equals(adminNo)) { 

      /** Create a TableRow dynamically **/ 
      tr = new TableRow(this); 

      /** Creating a TextView to add to the row **/ 
      label = new TextView(this); 
      label.setText(String.valueOf(j)); 
      label.setId(p.getId()); 

      acd = String.valueOf(p.getId()); 
      label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 
      label.setPadding(5, 5, 5, 5); 
      label.setBackgroundColor(Color.GRAY); 

      tr.addView((label); // Adding textView to tablerow. 
    } 
} 

しかし、あなたはのLinearLayoutを追加する必要がある場合、あなたはこれが動作するはずです。この

public void filter(TableLayout tl, String regex) { 
    TableRow tr; 
    TextView tv; 
    Pattern p; 
    Matcher m; 

    p = Pattern.compile(regex); 
    int n = tl.getChildCount(); 
    for (int i = 0; i < n; i++) { 
     tr = (TableRow) tl.getChildAt(i); 
     LinearLayout ll = (LinearLayout) tr.getChildAt(0); 
     tv = (TextView) ll.getChildAt(0); 
     m = p.matcher(tv.getText()); 
     if (m.find()) { 
      tr.setVisibility(View.VISIBLE); 
     } else { 
      tr.setVisibility(View.GONE); 
     } 

を行うことでのTextViewを取り戻す必要があります:あなたにできることはこれです。うまくいけばそれは役に立ちます、goodluck

+0

フィルタの後に別の質問にリンクすることは可能ですか?テーブルのヘッダーがなくなり、メソッドを追加すると、検索されたデータの下にヘッダーが置かれます。 –

+0

問題はありません。リンクしてくださいそれに答えてみてください – shegzy

+0

https://stackoverflow.com/questions/45518233/header-shows-only-at-the-end-of-the-table-layout-in-android –

関連する問題