2012-02-19 9 views
1

私は値/項目リストを動的に印刷する設定プログラムを構築しています。場合によっては、名前などの出力項目が1つありますが、IPアドレス、ユーザー名、パスワードなど、出力する項目が複数ある場合もあります。カスタムリストアダプタの動的に作成されたtextviewsのAndroid findViewByIdが機能していないようです

アイデアは、出力されるパラメータの量に応じてリストを動的にアップする。小さな画面では、パラメータの量は2になるかもしれませんが、風景モードやタブレットではパラメータの量がはるかに多くなることがあります。

私はAndroidのIDを持つハードコード化されたXMLファイルを持っているとき、私は、すなわち

android:id="@+id/item1 

を参照するが、私はプログラム的にこれをやって苦労してる、この作業を持っています。編集する前にIDでこれをやっていましたが、アドバイスを受けてタグでこれを試しています。

ここにコードのすべての関連部分があります。パラメータの複数行のリストがbuildLayoutに、我々は枝カスタムレイアウトを構築するために出力されます。

ここ
public void processAsyncTask(String result) {  
     ConfigList list = getConfigList(result); 
     ArrayList<ConfigCollection> mParamsList = list.getAllItems(); 
     // Once we have a list of parameters we build the layout according to size 
     buildLayout(mParamsList.size()); 
     mMultiRowAdapter = new ParamsAdapter(this, R.layout.multi_row_config, mParamsList);   
     mCollectionListView.setAdapter(mMultiRowAdapter);   
     } 

はbuildLayoutは次のとおりです。

private void buildLayout(int size) {     
    TableRow tr = (TableRow)findViewById(R.id.row_multi_row);      
    for (int i = 0; i < size - 1; i++) { 
     TextView tv = new TextView(this); 
     tv.setId(i); 
     tv.setTag("id" + String.valueOf(i)); 
     tv.setTextAppearance(this, android.R.style.TextAppearance_Large); 
     tv.setText("id" + String.valueOf(i)); 
     tr.addView(tv); 
    }    
} 

そして、ここでは、私はを参照しようとしている方法ですIDは:ここでは

/** 
    * Custom Array Adapter with a view that displays multi-line item/value pairs 
    */ 
    private class ParamsAdapter extends ArrayAdapter<ConfigCollection> { 

     private ArrayList<ConfigCollection> objectList; 

     public ParamsAdapter(Context context, int textViewResourceId, ArrayList<ConfigCollection> objectList) { 
      super(context, textViewResourceId, objectList); 
      this.objectList = objectList; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 

      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.multi_row_config, null);    

      // It works with hard coded IDs, e.g.: 
      TextView tv1 = (TextView) v.findViewById(R.id.item1); 
      TextView tv2 = (TextView) v.findViewById(R.id.item2); 

      // BELOW DOES NOT, tv1 and tv2 is null after assignment 
      TextView tv1 = (TextView) v.findViewWithTag("id0"); 
     TextView tv2 = (TextView) v.findViewWithTag("id1"); 

は、私がテストに使用していたXMLファイルです:

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout  
    android:id="@+id/table_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
    /> 

    <TableRow 
    android:id="@+id/row_multi_row" 
    > 

    <TextView 
     android:id="@+id/item1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"     
     android:textAppearance="?android:attr/textAppearanceLarge"    
     android:text="item1"  
     android:padding="3dip" 
    /> 

    <TextView 
     android:id="@+id/item2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"     
     android:textAppearance="?android:attr/textAppearanceLarge"    
     android:text="item2" 
     android:padding="3dip" 
    /> 

    </TableRow> 

</TableLayout> 

EDIT: 私はこのスクリーンショットでさらに問題を確認しました。 buildLayoutではtextviewsを追加しているようですが、カスタム配列アダプタではlayoutinflatorがこれらのフィールドを見つけられません。また、IDの代わりにタグを使って作業するようにコードを更新しました。

buildLayoutからデバッグします。 4人の子供を注意してください、XMLコードに固定された2つの2つはParamsAdapterから

Debug from buildLayout. Notice four children, the two fixed in the XML code and two created

デバッグを作成しました。実際にこれらのオブジェクトにアクセスしようとしているこの段階では、2人の子供に注意してください。

Debug from ParamsAdapter. Notice only two children at this stage

+0

あなたはtv.setId( " sometext "+ i); sometextをIDに追加します。私はsomeIDと矛盾するかもしれないと設定していると思います。 – kosa

+0

@thinjksteepなので、setIdをStringと一緒に使うことができますか? – Selvin

+0

@Selvin文字列パラメータとともに使用しようとすると、正しいsetIdのエラーが発生します。 –

答えて

1

あなたがプログラムでアイテムを作成している場合、私は「タグ」パラメータを使用して助言します。あなたが親ビューを持っている限り、 "findViewWithTag"を見ることができます - http://developer.android.com/reference/android/view/View.htmlを参照 - findWithTagにスクロールダウン

+0

ご返信ありがとうございます。私はこれを試して、 'tv.setTag(i)'を追加し、その後 'TextView tv1 =(TextView)v.findViewWithTag(0)'を実行して参照しましたが、tv1はまだnullに終わります。私はそれが私が物事をしている順序ではないかと思っています、それは私がそこにない何かを参照しているようなものです... –

+0

文字列でタグを設定し、文字列で取得しようとしますか? –

+0

私は '' id "+ String.valueOf(i)'を使って試みましたが、運はありません。私はXMLファイルを疑問に思っています。私はlayoutinflatorがlistview内のtextviewsで動作すると思っていますが、私はlistviewの外で新しいtextviewsを作成しています。おそらく私は子供を参照する必要がありますが、どのように?インフレータサービスがハードコードされたXML **と**動的に追加されたtextviewsで動作するかどうかも私は疑問に思っています。 –

関連する問題