开发者

Android :Implementing a ListView using a CustomAdapter

I am trying to populate a ListView using a CustomAdapter. I want to give a separate layout for each ListItem. So for this I overrid the getView() method of my CustomAdapter as follows.

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;
}

However , when I try debugging the line myview=View.inflate(mycontext,R.layout.customrow, parent);

there seems to be a problem. The Debugger opens up ListView.java class and then that file throws a InvocationTargetException which opens up ZygoteInit class and then nothing happens.

I dont seem to understand why myview is not able to get inflated with an xml file .

I need some help on this .


Try using LayoutInflator instead:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜