2

私はリストビューを持つ複雑なxmlレイアウトを持っています。リストビューの行には、複数のテキストフィールドが均等に配置されています。私はtextviewを使用してテキストを保存し、最後に行にすべての項目を追加しています...その完璧に動作します。 しかし、今私は確信していない場合、どのくらいのテキストフィールドを私はWebサービスから得るかもしれないケースがあります。したがって、私は実行時に動的にtextviewを作成し、それらを入力し、リストに挿入する必要があります。 実行時に新しいtextviewフィールドを宣言し、追加して設定するにはどうにかしていますか? 2つのフィールドの間隔を実装する方法はありますか?実行時にレイアウトを作成する

最初の呼び出しの結果

__________________________ 
|_____|_____|_____|______| 

2回目の呼び出しの結果

________________________________ 
|_____|_____|_____|______|______| 

私は(ケニー)の下に提供されたソリューションを実装しようとしたが、何らかの理由で、私はにビューを追加することができませんリストは..私のコードです

public class HeaderAdapter extends ArrayAdapter<Header> { 
final Header[] listSymbols; 
private TextView textView; 
private LinearLayout row; 

public HeaderAdapter(Context context, int symResourceID, 
     Header[] objects) { 
    super(context, symResourceID, objects); 
    listSymbols = objects; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) getContext() 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View row = inflater.inflate(R.layout.header_view, parent, false); 
    Header headerRec = listSymbols[position]; 
    for(int i = 0 ; i < listSymbols.length;i++){ 
     textView = new TextView(getContext()); 
     textView.setLayoutParams(new LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view 
       ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view 
     textView.setId(i); 
      row.add?? 

     } 
} 

これを呼び出す主な活動

setContentView(R.layout.main); 

    headerList.add(new Header("Symbol","Quantity","Price","Price Yesterday","52 Week High","52 Week Low","Change","Days Gain","Days Gain %","Returns")); 

      Header[] tmpHeaderList = headerList.toArray(new Header[headerList.size()]); 

      ArrayAdapter<Header> headerAdapter = new HeaderAdapter(this,R.layout.twoway_header_view,tmpHeaderList); 
      headerListView.setAdapter(headerAdapter); 

のxmlレイアウトがメインファイル

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <HorizontalScrollView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:scrollbars="none" 
     android:id="@+id/headerHv"> 

     <ListView android:id="@+id/header_listView1" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:background="@color/white" android:cacheColorHint="#00000000" 
      android:smoothScrollbar="true" android:scrollbars="none" /> 
    </HorizontalScrollView> 
</LinearLayout> 

行のためのテンプレートが

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
    <TextView android:id="@+id/headerList" android:layout_width="80dp" 
     android:layout_height="wrap_content" android:textColor="#000000" 
     android:typeface="sans" android:textStyle="normal" /> 

</LinearLayout> 

答えて

4

iは動的リストからカスタムボタンを生成する方法です、あなたはtextViewsと同じことを行うことができます:

//Setup Buttons 
    layout = (LinearLayout)findViewById(R.id.layoutBars); 
    int count = lBars.size(); 
    for(int i = 0; i< count;i++){ 
     final Bar b = lBars.get(i); 
     BarButton button = new BarButton(DDTBars.this, R.drawable.barsmall , b.getName().toUpperCase()); 
     button.setLayoutParams(new LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT)); 
     button.setId(i); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //Run activity passing name of the bar to retrieve data 
       Intent i = new Intent(DDTBars.this, DDTBar.class); 
       i.putExtra("name", b.getName()); 
       startActivity(i); 
      } 
     }); 
     layout.addView(button);   
    } 

だからあなたが試みることができる:

//Setup TextViews 
    layout = (LinearLayout)findViewById(R.id.mylayout); 
    int count = myTextList.size(); 
    for(int i = 0; i< count;i++){ 
     TextView txtView = new TextView(this); 
     txtView.setLayoutParams(new LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view 
       ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view 
     txtView.setId(i); 
     layout.addView(txtView);    
    } 
+0

@Kennyを...ありがとう...しかしここであなたはBarButtonを使用していますが、textviewのようなものはありますか? –

+0

BarButtonは私が作成したカスタムボタンで、すべてのボタンはViewクラスを拡張し、textViewもビュークラスを拡張します。まったく同じことができますが、ボタンではなくtextViewを追加することができます。 – Kenny

+1

私は自分の答えを拡大して表示しました。質問に答えてマークして、回答のないリストから削除してください。 – Kenny

0

あなたはコードでそれを行う可能性が定義されているファイルをfile..the。 TextViewをループで宣言し、RelativeLayoutを使用してそれらを互いに配置します。ここで

関連する問題