0

我正在尝试为此显示一个列表,我使用了一个类 Ios.java

public class Ios extends Activity implements OnItemClickListener,OnClickListener


        {
        list=(ListView) findViewById(R.id.list);
        list.setOnItemClickListener(this);
        String DATA[] = {"io1","io2","io3","io4","io5"};
        IosListAdapter obj = new IosListAdapter(this.getApplicationContext()
                ,R.layout.row, DATA);
        list.setAdapter(obj);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }}

我的自定义适配器如下:

public IosListAdapter(Context con, int rid,String[] dATA) {
        // TODO Auto-generated constructor stub
        this.con = con;
        this.DATA = dATA;
        this.rid = rid;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater loi = LayoutInflater.from(con);          
        View v = loi.inflate(rid, parent, false);   
        ImageView iv= (ImageView) v.findViewById(R.id.t1);
        TextView tv1 = (TextView) v.findViewById(R.id.t4);
        iv.setBackgroundResource(arrow);
        tv1.setText(DATA[position]);
        return v;
    }}

和 demolist.xml::

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


    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:stretchColumns="1" android:gravity="center_vertical"
        android:background="@drawable/toptab" android:id="@+id/tb1">

        <TableRow>

            <Button android:id="@+id/btnback" android:background="@color/hover_bckbtn"
                android:text="Back" android:textColor="#ffffff" android:textSize="14sp"
                android:textStyle="normal" android:typeface="normal"
                android:layout_marginLeft="5dp" />


            <TextView android:id="@+id/tvnormalcallreg" android:text="Call Registration"
                android:textColor="#ffffff" android:textSize="18sp"
                android:layout_gravity="center" android:textStyle="normal"
                android:typeface="normal" />

            <Button android:id="@+id/btnregister" android:textColor="#ffffff"
                android:background="@color/hover_button" android:text="Register"
                android:textSize="14sp" android:textStyle="normal" android:typeface="normal"
                android:layout_marginRight="5dp" />
        </TableRow>
    </TableLayout>


    <TableLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:stretchColumns="0"
        android:layout_marginRight="30dp">

        <TableRow android:gravity="center_vertical"
            android:layout_width="fill_parent">

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

                <TextView android:id="@+id/top" android:layout_height="wrap_content"
                    android:textStyle="bold" android:layout_width="wrap_content"
                    android:singleLine="true" android:textColor="#ffffff"
                    android:typeface="normal" />

                <TextView android:id="@+id/address" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:textSize="10sp"
                    android:textColor="#ffffff" android:typeface="normal"
                    android:layout_marginTop="-2dp" />

                <TextView android:id="@+id/tvdept" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:textSize="10sp"
                    android:textColor="#ffffff" android:typeface="normal"
                    android:layout_marginTop="-2dp" />

            </LinearLayout>


        </TableRow>

    </TableLayout>



    <ListView android:id="@+id/list" 
        android:layout_below="@+id/toptbl"  
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" 
        android:layout_width="fill_parent" 
        android:cacheColorHint="#00000000"
        android:fadingEdge="none"
        android:layout_marginBottom="50dp"/>

    <TableLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:id="@+id/bottom">

        <TableRow android:background="@drawable/toptab"
            android:gravity="center">

            <ImageView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:background="@drawable/logobottom" />
        </TableRow>

    </TableLayout>



</RelativeLayout>

它运行良好我面临的唯一问题是列表没有显示,当我调试它时,我发现适配器的 getView 方法从未被调用。

4

2 回答 2

5

您的getCount方法应根据您提供给适配器的数据返回您拥有的行数。像这样的东西:

@Override
public int getCount() {
    return DATA.length;
}
于 2012-09-20T13:05:33.977 回答
0

它对上述答案有所改进。您正在扩展的 Adapter 类是什么?我在您的构造函数中看不到对 super 的任何调用。我猜你正在扩展 ArrayAdapter,或者什么。你必须像这样修改你的 getCount() :

@Override
    public int getCount() {

    return (DATA == null) ? 0 : DATA.length;
    }
于 2012-09-20T13:21:55.283 回答