Android ListView BUG?

时间:2012-02-16 10:53:19

标签: android listview

我正在开发Android 2.3.3(Gingerbread),我有这个特殊的活动,在ListView中显示50个项目。问题是,当活动开始时,当我开始在ListView中向下滚动时,列表视图不会正确显示项目。列表视图中存在误导性元素,其中位置12应显示“标题:12”,“元素:12”而不是“标题:xx”,“元素:xx”,其中xx - > 1< = xx< = ELEMENT_SIZE。但是,当我点击该位置时,它会显示正确的元素。

xml和源代码位于下方。

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

child.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/childTextTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/childTextDetail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

ListViewDebugActivty

public class ListViewDebugActivity extends Activity {

    private final static int ELEMENT_SIZE = 50;
    private ListView listView;
    private List<Element> elements;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initialize();
        fillListView();
    }

    private void initialize() {     
        listView = (ListView) findViewById(R.id.listView);
        elements = new ArrayList<Element>();
    }

    private void fillListView() {
        final String elementTitle = "Title: ";
        final String elementDetail = "Detail: ";

        for(int index = 0; index < ELEMENT_SIZE; index ++) {
            elements.add(new Element(elementTitle + (index + 1), elementDetail + (index + 1)));
        }

        listView.setAdapter(new ListViewAdapter(this, R.layout.child, elements));
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                Element element = elements.get(position);
                String message = position + " = " + element.getTitle() + " : " + element.getDetail();
                Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
            }
        });
    }

    private class Element {

        private String title, detail;

        public Element(String title, String detail) {
            this.title = title;
            this.detail = detail;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDetail() {
            return detail;
        }

        public void setDetail(String detail) {
            this.detail = detail;
        }

    }

    private class ListViewAdapter extends ArrayAdapter<Element> {

        private List<Element> objects;
        private TextView childTitle, childDetail;

        public ListViewAdapter(Context context, int layout, List<Element> objects) {
            super(context, layout, objects);
            this.objects = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Element element = objects.get(position);
            if(convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.child, null);
                childTitle = (TextView) convertView.findViewById(R.id.childTextTitle);
                childDetail = (TextView) convertView.findViewById(R.id.childTextDetail);
            }
            if(element != null && convertView != null) {
                childTitle.setText(element.getTitle());
                childDetail.setText(element.getDetail());
            }

            return convertView;
        }

    }

}

提前致谢。

2 个答案:

答案 0 :(得分:2)

我不会讲授ViewHolders,但是现在,在getView方法中移动childTitle和childDetail实例应该可以解决你遇到的问题。 如果您在列表视图中发现想要更高的速度和效率,请单击链接。

答案 1 :(得分:0)

更改行:

if(element != null && convertView != null) {
                childTitle.setText(element.getTitle());
                childDetail.setText(element.getDetail());
            }

if(convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.child, null);

        }
        if(onvertView != null)
        {
             childTitle = (TextView) convertView.findViewById(R.id.childTextTitle);
             childDetail = (TextView) convertView.findViewById(R.id.childTextDetail);
             if(element != null ) {
                   childTitle.setText(element.getTitle());
             }
        }