0

我试图实现简单的自定义列表视图。我试图做的是在列表视图中的每个列表行内膨胀一个文本和一张图片。我没有输出,即布局没有膨胀..我得到一个空白屏幕。我使用了 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>
4

2 回答 2

1

在 CustomListViewAdapter 中,更改此项

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

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

于 2013-09-07T14:10:57.870 回答
0
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

你回来了0

退货pic.lengthshape1.length根据您的要求。

public int getCount() {

    return pic.length;
}

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

获取计数()

于 2013-09-07T14:14:06.943 回答