2011-09-12 11 views
1

私は私のViewクラスをTextViewにするために、文字列配列の値をapplayしたいandroid.iの新しい開発者はそのビュークラスで、ビュークラスを作成していますが、次のされていますビュークラスからビューを取得する方法は?

class MyView extends View 
{ 
    Context con; 
String messages[]=new String[]{"hello","hai","how are you","where are you"}; 
public MyView(Context context) { 
    super(context); 
    con=context; 

     for(int i=0;i<3;i++) 
    { 

     Log.v("messages", "messages"+messages[i]); 
    } 


    public View getView(int position, View view, ViewGroup vg) 
{ 
    if (view == null) 
    { 
     // context would be a variable set via the constructor and given 
     // by our owner (most likely a ListActivity) 
     LayoutInflater inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.view, null); 
    } 

    // Here you would get the sub-views of your layout and fill them 
    TextView msg = (TextView) view.findViewById(R.id.txtmessage); 
    msg.setText(messages[position]); 
    return view; 
} 

}

を上からクラスIは、上記のコードからアクティビティクラス

 public class ViewActivity extends Activity { 



TableRow tbr; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view); 

    tbr=(TableRow)findViewById(R.id.tableRow1); 


    MyView mv=new MyView(this); 
    tbr.addView(mv); 

} 

}

にMYVIEWクラスのビューを表示したい私どのように私はあなたがあなたのビューにlayoutparamsを設定する必要がMYVIEWクラス

答えて

2

のビューを取得することができますMYVIEWクラス

で表示することはできませんよ。今あなたのビューはサイズがありません。

あなたはこの

MyView mv=new MyView(this); 
mv.setLayoutParams(new LayoutParams(new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT))); 
tbr.addView(mv); 
ようなものが必要
関連する問題