0

嘿,我试图让一个列表工作,在这种情况下,我有一个 xml 定义的项目,我在扩展的 ArrayAdapter 中膨胀。但是在我膨胀了 xml 之后,当我尝试抓取 imageview 时,它会通过 findviewbyid 返回 null ...这是该项目的 xml:

<?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="wrap_content"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/product_item_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="if no image avail show this"
        android:visibility="gone"/>
    <ImageView 
        android:id="@+id/product_item_imageview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        />

</LinearLayout>

实例化项目的方法:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SimpleListItem item= items.get(position);
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.product_item, null);
        if(item.getPicture()!=null)
        {
            ImageView imgVw = (ImageView) findViewById(R.id.product_item_imageview);
            String picturePath = item.getPicture();
            Bitmap picture = ImageManager.get(getBaseContext(), picturePath);
            imgVw.setImageBitmap(picture);
        } else {
            TextView txtVw = (TextView) findViewById(R.id.product_item_textview);
            txtVw.setText(item.getText());
            txtVw.setVisibility(View.VISIBLE);
        }
        return v;
    }
4

2 回答 2

3

它是,

  ImageView imgVw = (ImageView) v.findViewById(R.id.product_item_imageview);

也一样TextView

 TextView txtVw = (TextView) v.findViewById(R.id.product_item_textview);

用于View v获取ImageViewTextView从膨胀的 xml 文件中获取。

于 2012-06-01T12:47:30.707 回答
1

//你在视图 v 中膨胀布局,所以你需要通过

ImageView imgVw = (ImageView)v. findViewById(R.id.product_item_imageview);


TextView txtVw = (TextView)v. findViewById(R.id.product_item_textview);
于 2012-06-01T12:48:56.813 回答