现在的位置: 首页 > 综合 > 正文

ListView 稍高级应用

2013年10月07日 ⁄ 综合 ⁄ 共 3314字 ⁄ 字号 评论关闭

1、布局文件(两个)

第一个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView    android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>
第二个:

<?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="horizontal">

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2px"
        ></ImageView>

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:id="@+id/title" android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22px"/>
        <TextView android:id="@+id/info" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="12px"/>
    </LinearLayout>
   
</LinearLayout>

2、Java代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new IconicAdapter(this));

    }

    class IconicAdapter extends ArrayAdapter {
        Activity context;

        public IconicAdapter(Activity context) {
            super(context, R.layout.staticitem, items);
            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View row = inflater.inflate(R.layout.staticitem, null);

            TextView label = (TextView) row.findViewById(R.id.title);
            label.setText(items[position]);
            if (items[position].length() > 4) {
                ImageView icon = (ImageView) row.findViewById(R.id.icon);
                icon.setImageResource(R.drawable.delete);
            }else
            {
                ImageView icon = (ImageView) row.findViewById(R.id.icon);
                icon.setImageResource(R.drawable.ok);
            }

            TextView label2 = (TextView) row.findViewById(R.id.info);
            label2.setText(items2[position]);           
           
            return row;
        }

         //使用下面的public View getView(int position, View convertView, ViewGroup parent) 方法,效率会提高,也会节约手     

         //机电源

         /*

           public View getView(int position, View convertView, ViewGroup parent) {
           
           
            View row = convertView;
           
            if(row == null)
            {
                LayoutInflater inflater = context.getLayoutInflater();
                row = inflater.inflate(R.layout.staticitem, null);
            }       

            TextView label = (TextView) row.findViewById(R.id.title);
            label.setText(items[position]);
            if (items[position].length() > 4) {
                ImageView icon = (ImageView) row.findViewById(R.id.icon);
                icon.setImageResource(R.drawable.delete);
            }else
            {
                ImageView icon = (ImageView) row.findViewById(R.id.icon);
                icon.setImageResource(R.drawable.ok);
            }

            TextView label2 = (TextView) row.findViewById(R.id.info);
            label2.setText(items2[position]);           
           
            return row;

          }

        */

    }

 

3、显示效果:

 

抱歉!评论已关闭.