ImageView设置图像资源不显示wanted image.png

时间:2013-12-14 01:14:00

标签: android gridview resources imageview gallery

在我的gridViewAdapter中,我将ImageResource设置为.png对象,但它没有显示在activity中。这是适配器的函数getView:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    ViewHolder holder; 
    if (row == null) { 
        LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
        row = inflater.inflate(layoutResourceId, parent, false); 
        holder = new ViewHolder(); 
        holder.photoSubscriptTextView = (TextView) row.findViewById(R.id.photoSubscriptTextView); 
        holder.photoInstanceImageView = (ImageView) row.findViewById(R.id.galleryMonumentPhotoImageView); 
        row.setTag(holder); 
    } else { 
        holder = (ViewHolder) row.getTag(); 
    } 
    final Photo photo = data.get(position); 
    row.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
            Intent intent; 
            if(photo.getPodpis().equals(getContext().getString(R.string.photo_add))){ 
                intent = new Intent(); 
                intent.setType("image/*"); 
                intent.setAction(Intent.ACTION_GET_CONTENT); 
                ((Activity) context).startActivityForResult(Intent.createChooser(intent, "Select Picture"), 100); 
            } 
            else { 
                intent = new Intent(context, PhotoGallery.class); 
                intent.putExtra("photo", photo); 
                context.startActivity(intent); 
            } 
        } 
    }); 
    holder.photoSubscriptTextView.setText(photo.getPodpis()); 
    if(photo.getUri() != null) 
        aQuery.id(holder.photoInstanceImageView).image(photo.getUri()); 
    else if(photo.getPodpis().equals(getContext().getResources().getString(R.string.photo_add)))  { 
        holder.photoInstanceImageView.setImageResource(R.drawable.image_add); 
    } 
    return row; 

和xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          xmlns:tools="http://schemas.android.com/tools" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginTop="5dp" 
          android:orientation="vertical" 
          android:padding="5dp" 
          android:clickable="true" 
          android:focusable="true"> 
<!--android:background="@drawable/grid_color_selector"--> 
<ImageView 
        android:id="@+id/galleryMonumentPhotoImageView" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:src="@drawable/image_add"> 
</ImageView> 
<TextView 
        android:id="@+id/photoSubscriptTextView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="5dp" 
        android:gravity="center" 
        android:textSize="12sp" > 
</TextView> 
</LinearLayout>

主布局仅包含Relative Layout和GridView。 有人能告诉我为什么我在适配器中设置的资源中的图像不会显示在活动中吗?

1 个答案:

答案 0 :(得分:0)

仅仅因为你将onClick()方法放在getView()中并不意味着它可以访问在getView()中创建的临时变量。单击的项可能与上次处理的getView()方法不同。所以,他们之间没有联系。

您应该在活动onCreate()或onCreateView()代码上的ListView本身上放置一个onItemClick()侦听器。该方法具有被单击的项的位置,然后您可以使用该位置从ArrayList中获取该项。

像这样:

mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long row_id) {
        final Photo photo = data.get(position); 
        // etc.
    }
}