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>
実際に何が起こっているのかわかるようにxmlファイルに投稿する必要がありますが、そのキャストの例外以降、期待通りの順番でビューが返されない可能性があります。親切にxmlファイルを投稿してください – shegzy
私はそれに応じて追加しました。もう一度見ていただけますか?必要に応じて、私はあなたが私を助けるために必要な他のコードを掲載します。 –