2011-06-30 8 views
0

CustomAdapterを使用してListViewを生成しようとしています。私は各ListItemのための別のレイアウトを与えたい。そこで、私はCustomAdapterのgetView()メソッドを次のようにオーバーライドします。しかしAndroid:CustomAdapterを使用してListViewを実装する

public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    // This is where our ListView is constantly going to ask the Adapter for all the Views that it needs 
    View myview; 
    ImageView pic; 


    TextView text; 

    if(convertView==null) 
    {  

     **myview=View.inflate(mycontext,R.layout.customrow, parent);** 
     pic=(ImageView)myview.findViewById(R.id.pic); 
     pic.setLayoutParams(new ListView.LayoutParams(100,100)); 
     //text=(TextView)myview.findViewById(R.id.text); 
     //text.setTextSize(14); 


    } 
    else 
     myview=convertView; 

    if(cache[position]==null) 
    { 
     BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inSampleSize=10; 
     Bitmap thumb=BitmapFactory.decodeResource(mycontext.getResources(), Images[position], options); 
     cache[position]=thumb; 

    } 
    pic=(ImageView)myview.findViewById(R.id.pic); 
    //text=(TextView)myview.findViewById(R.id.text); 
    pic.setImageBitmap(cache[position]); 
    //text.setText(titles[position]); 



    return myview; 
} 

、私はラインMYVIEW = View.inflate(あるMyContext、R.layout.customrow、親)をデバッグしてみてください。

問題があるようです。デバッガがListView.javaクラスを開き、そのファイルがInvocationTargetExceptionをスローすると、ZygoteInitクラスが開き、何も起こりません。

私はなぜmyviewがxmlファイルで膨らんで取得できないのか分かりません。

私はこれについていくつかの助けが必要です。

答えて

0

代わりLayoutInflatorを使用してみてください:

public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    // This is where our ListView is constantly going to ask the Adapter for all the Views that it needs 
    View myview; 
    ImageView pic; 


    TextView text; 

    if(convertView==null) 
    {  
     LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     myview=li.inflate(R.layout.customrow, null); 
     pic=(ImageView)myview.findViewById(R.id.pic); 
     pic.setLayoutParams(new ListView.LayoutParams(100,100)); 
     //text=(TextView)myview.findViewById(R.id.text); 
     //text.setTextSize(14); 


    } 
    else 
     myview=convertView; 

    if(cache[position]==null) 
    { 
     BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inSampleSize=10; 
     Bitmap thumb=BitmapFactory.decodeResource(mycontext.getResources(), Images[position], options); 
     cache[position]=thumb; 

    } 
    pic=(ImageView)myview.findViewById(R.id.pic); 
    //text=(TextView)myview.findViewById(R.id.text); 
    pic.setImageBitmap(cache[position]); 
    //text.setText(titles[position]); 



    return myview; 
} 
関連する問題