Android: реализация ListView с использованием CustomAdapter - PullRequest
0 голосов
/ 30 июня 2011

Я пытаюсь заполнить ListView, используя CustomAdapter. Я хочу дать отдельный макет для каждого ListItem. Поэтому для этого я переопределил метод getView () моего CustomAdapter следующим образом.

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, parent);

Кажется, есть проблема. Отладчик открывает класс ListView.java, а затем этот файл создает исключение InvocationTargetException, которое открывает класс ZygoteInit, и тогда ничего не происходит.

Кажется, я не понимаю, почему myview не может надуться с помощью XML-файла.

Мне нужна помощь в этом.

1 Ответ

0 голосов
/ 04 июля 2011

Попробуйте использовать 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...