ListView中的一个项目根本不显示(最初和重用时)

时间:2014-03-13 10:12:32

标签: android listview android-listview

我使用ArrayAdapter方法使用自定义getView

public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
        {
        LayoutParams lp;

            convertView = new RelativeLayout(context);
            convertView.setLayoutParams(
                new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 60));

            //--> line
            //----> serial
            TextView serial = new TextView(context);
            serial.setId(0x01);
            serial.setTextColor(Color.WHITE);
            serial.setGravity(Gravity.CENTER);
            lp = new LayoutParams(parent.getWidth()>>3,(int)(75*coeff_height));
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            serial.setLayoutParams(lp);
            serial.setTag("serial");
            ((RelativeLayout)convertView).addView(serial);
            //----> profil
            TextView profil = new TextView(context);
            profil.setId(0x02);
            profil.setTextColor(Color.WHITE);
            profil.setGravity(Gravity.CENTER);
            lp = new LayoutParams(parent.getWidth()>>3,(int)(75*coeff_height));
            lp.addRule(RelativeLayout.RIGHT_OF, serial.getId());
            profil.setLayoutParams(lp);
            profil.setTag("profil");
            ((RelativeLayout)convertView).addView(profil);
            //----> modified
            TextView modified = new TextView(context);
            modified.setId(0x08);
            modified.setTextColor(Color.WHITE);
            modified.setGravity(Gravity.CENTER);
            lp = new LayoutParams(parent.getWidth()>>3,(int)(75*coeff_height));
            lp.addRule(RelativeLayout.RIGHT_OF, created.getId());
            modified.setLayoutParams(lp);
            modified.setTag("modified");
            ((RelativeLayout)convertView).addView(modified);
        }

        //set content
        ((TextView)convertView.findViewWithTag("serial")).setText(""+position);
        ((TextView)convertView.findViewWithTag("profil")).setText("profil");
        ((TextView)convertView.findViewWithTag("modified")).setText("Dernière modification");
        System.out.println("END Position "+position+" // "+convertView);

        return convertView;
}

当显示我的ListView时,不显示第一个项目(最初)或与第一个View(再循环视图)的重复使用相对应的项目。视图的内容为空。所有其他视图都正确显示。

对这个问题有任何想法吗?

编辑:更多信息,请点击17:40 13 mars(UTC + 1)

我简化了这样的源代码:

public View getView(int position, View convertView, ViewGroup parent) {

    System.out.println("parent = "+parent.getClass());

    if(convertView == null)
    {
        LayoutParams lp;

        convertView = new RelativeLayout(context);
        //----> serial
        TextView serial = new TextView(context);
        serial.setId(0x01);
        serial.setTextColor(Color.WHITE);
        serial.setGravity(Gravity.CENTER);
        lp = new LayoutParams(parent.getWidth()>>3,(int)(75*coeff_height));
        serial.setLayoutParams(lp);
        serial.setTag("serial");
        ((LinearLayout)convertView).addView(serial);
    }

    //set content
    ((TextView)convertView.findViewWithTag("serial")).setText(""+position);

    return convertView;
}

问题仍然存在。

如果我更改并使用此代码,请输入:

public View getView(int position, View convertView, ViewGroup parent) {

    System.out.println("parent = "+parent.getClass());

    if(convertView == null)
    {
        LayoutParams lp;

        convertView = new TextView(context);
    }

    //set content
    convertView.setText(""+position);

    return convertView;
}

没有问题。所有项目都正确显示。在第一次调用getView期间,似乎问题来自实例化ViewGroup对象。我将尝试膨胀xml布局。但正如我在之前的评论中所说,我需要动态指定子视图的高度和宽度。

亲切, CC

0 个答案:

没有答案