文本在android 2.3.6的listview中消失

时间:2015-02-16 15:55:08

标签: android listview android-listview

我在我的项目中使用了膨胀的ListView。布局文件是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background2" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:cacheColorHint="#00000000"
        android:fastScrollEnabled="true"
        android:smoothScrollbar="true" >

    </ListView>

</RelativeLayout>

膨胀的布局文件是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/contact" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:text="Large Text"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Medium Text"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <CheckBox
                    android:id="@+id/checkBox1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="20dp" />

            </LinearLayout>  
        </LinearLayout>   
    </LinearLayout>    
</LinearLayout>

上面的代码在我使用API​​级别11或更高级别时工作正常,但在API级别11以下,它表现得很尴尬,就像textView1变得完全不可见而textView2仍然可见。此外,在滚动时,所有项目都是可见的,但是当滚动完成时,它再次变为不可见。我正在使用Actionbarsherlock。是的,最后一件事,我在动作栏中使用了SearchView,其底部奇怪地看起来像API 8中的蓝色突出显示的行,而一切都在更高的Android级别中完美运行

修改

这是我的ListView适配器的代码片段

private class ContactViewHolder {
        CheckBox checkBox;
        TextView textView;
        TextView textView2;
        ImageView iv;
}

private class ContactArrayAdapter extends ArrayAdapter<Contact> {

        //private LayoutInflater inflater;
        List<Contact> myList;
        Context mContext;
        //Cursor cursor;

        public ContactArrayAdapter(Context context, List<Contact> planetList) {
            super(context, R.layout.simplerow, R.id.textView1, planetList);
            // Cache the LayoutInflate to avoid asking for a new one each time.
            //inflater = LayoutInflater.from(context);
            mContext = context;
            myList = planetList;
        }

        @Override
        public int getCount() {
            if(myList != null)
                return myList.size();
            return 0;
        }

        @Override
        public Contact getItem(int position) {
            if(myList != null)
                return myList.get(position);
            return null;
        }

        @Override
        public long getItemId(int position) {
            if(myList != null)
                return myList.get(position).hashCode();
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Contact to display

            ContactViewHolder holder;

            //If the listview does not have an xml layout ready set the layout
            if (convertView == null){

                //we need a new holder to hold the structure of the cell
                holder = new ContactViewHolder();

                //get the XML inflation service
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                //Inflate our xml cell to the convertView
                convertView = inflater.inflate(R.layout.simplerow, null);

                //Get xml components into our holder class

                holder.textView = (TextView) convertView.findViewById(R.id.textView1);
                holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
                holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);

                holder.checkBox.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Contact contact = (Contact) cb.getTag();
                        contact.setChecked(cb.isChecked());
                    }
                });

                //Attach our holder class to this particular cell
                convertView.setTag(holder);

            }else{

                //The listview cell is not empty and contains already components loaded, get the tagged holder
                holder = (ContactViewHolder) convertView.getTag();

            }

            //Fill our cell with data

            //get our person object from the list we passed to the adapter
            Contact contact = getItem(position);

            //Fill our view components with data
            holder.textView.setText(name1.get(position));
            holder.textView2.setText(num1.get(position));
            holder.checkBox.setChecked(contact.checked);

            return convertView;
        }
    }

Contact类是

private static class Contact {
        private String name = "";
        private boolean checked = false;

        public Contact() {
        }

        public Contact(String name) {
            this.name = name;
        }

        public Contact(String name, boolean checked) {
            this.name = name;
            this.checked = checked;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String toString() {
            return name;
        }

        public void toggleChecked() {
            checked = !checked;
        }
    }

1 个答案:

答案 0 :(得分:0)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/contact" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="fill_vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp" />

    </LinearLayout>
</LinearLayout>