Customlistview不膨胀

时间:2013-09-07 14:07:21

标签: android

我试图实现简单的customlistview。我试图做的是在listview中的每个列表行内膨胀文本和图片。我没有输出,即布局没有膨胀......我有一个空白的屏幕。我使用了2个Java类和2个xml文件。下面是我的代码..请告诉我我的错误在哪里以及我应该纠正什么?

CListView.java

package com.example.customlistview;
public class Clistview extends Activity {

int p1[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};

String[] Shapes1={"circle","circle","circle","circle","circle"};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clistview);
    lv=(ListView)findViewById(R.id.listView1);
            CustomListViewAdapter clvadapter=new CustomListViewAdapter(Clistview.this,Shapes1,p1);
    lv.setAdapter(clvadapter);
}
}

CustomListViewAdapter.java

package com.example.customlistview;

public class CustomListViewAdapter extends BaseAdapter {

Context context;
String shape1[];
int pic[];
public CustomListViewAdapter(Context c,String[] sh,int[] p)
{
    this.context=c;
    this.shape1=sh;
    this.pic=p;
}

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

public String getItem(int pos) {
    // TODO Auto-generated method stub
    return shape1[pos];
}

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

public View getView(int pos, View v, ViewGroup vg) {
    // TODO Auto-generated method stub
    View itemview=v;
    TextView tv1;
    ImageView im1;
    if(itemview==null)
        {
   LayoutInflater inflater=(LayoutInflater)((Activity)context).getLayoutInflater(); 
    itemview=inflater.inflate(R.layout.clistview1,vg,false);
    }
    tv1=(TextView)itemview.findViewById(R.id.textView1);
    im1=(ImageView)itemview.findViewById(R.id.imageView1);
    tv1.setText(shape1[pos]);
    im1.setImageResource(pic[pos]);

    return itemview;
}

 }

activity_clistview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Clistview" >
<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp" >
</ListView>

</RelativeLayout>

clistview1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
 > 
<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="right"
  />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

在CustomListViewAdapter中,更改此

public int getCount() {
    // TODO Auto-generated method stub
    return shape1.length;
}

这是决定ListView中项目数量的重要功能。

答案 1 :(得分:0)

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

您将返回0

根据您的要求返回pic.lengthshape1.length

public int getCount() {

    return pic.length;
}

方法getCount()返回适配器数据集中的项目数。

getCount()