为什么ListView为空?

时间:2014-10-23 15:01:51

标签: java android xml listview android-listview

我正在尝试制作一个 listView ,其中包含仅包含没有文字的图片,但它会显示一个白色屏幕并且我没有收到任何错误或任何错误。

以下是我的代码:

的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="3">

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2">

    <ListView 
       android:id="@+id/setOneListView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center" />

</RelativeLayout>

   .........

</LinearLayout>
</LinearLayout>

活动:

public class Set1_Activity extends Activity {

ListView list;
int[] images = {
        R.drawable.iv1, R.drawable.iv2, R.drawable.iv3, R.drawable.iv4,
        R.drawable.iv5, R.drawable.iv6, R.drawable.iv11, R.drawable.iv12,
        R.drawable.iv13, R.drawable.iv14, R.drawable.iv15, R.drawable.iv16,
        R.drawable.iv111, R.drawable.iv112, R.drawable.iv113, R.drawable.iv114,
        R.drawable.iv115, R.drawable.iv116, R.drawable.iv7, R.drawable.iv8, 
        R.drawable.iv9, R.drawable.iv19, R.drawable.iv49, R.drawable.iv50,
        R.drawable.iv52, R.drawable.iv54
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main2);

    list = (ListView) findViewById(R.id.setOneListView);
    HeartlessAdapter adapter = new HeartlessAdapter(this, images);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            switch(position){
               case 0:
                   display.setImageResource(R.drawable.iv1);
                tophone = R.drawable.iv1;
                break;
           .........

class HeartlessAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;

HeartlessAdapter(Context c, int imgs[])
{
    super(c, R.layout.imageview);
    this.context=c;
    this.images=imgs;
}

class MyViewHolder
{
    ImageView myImage;

    MyViewHolder(View v)
    {
        myImage = (ImageView) v.findViewById(R.id.imageView);

    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView; 
    MyViewHolder holder = null;
    if(row==null)
    {
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       row = inflater.inflate(R.layout.imageview, parent, false);
       holder = new MyViewHolder(row);
       row.setTag(holder);
    }
    else
    {
        holder = (MyViewHolder) row.getTag();
    }

    holder.myImage.setImageResource(images[position]);


    return row;
}
}

一切正常,没有错误,模拟器只显示空白屏幕。可能会遗漏什么?

谢谢!

3 个答案:

答案 0 :(得分:1)

尝试将android:layout_weight="2"向上移动到水平LinearLayout。

答案 1 :(得分:0)

试试这个......

 runOnUiThread(new Runnable() {
                public void run() {
                    adapter.notifyDataSetChanged()
                }
            });

答案 2 :(得分:0)

1)让HeartlessAdapter扩展ArrayAdapter<Integer>而不是ArrayAdapter<String>

2)在你的构造函数上,调用super(c, R.layout.imageview, imgs);它应该像那样工作。

3)如果你想要优化它,请删除对Context的引用和适配器中的图像数组。您可以通过调用getItem(position)来获取调用getContext()和上下文所需的任何项目。

编辑:

为了使其正常工作,请致电:

super(c, R.layout.imageview, HeartlessAdapter.toIntegerArray(imgs));

并将此方法添加到HeartlessAdapter

private static Integer[] toIntegerArray(int[] array){
    Integer[] finalArray = new Integer[array.length];
    for (int i=0; i<array.length; i++){
        finalArray[i] = array[i];
    }
    return finalArray;
}