簡體   English   中英

ImageView在android中的自定義ArrayList適配器中保持NULL

[英]ImageView stays NULL in custom ArrayList adapter in android

我正在嘗試在自定義ArrayAdapter中訪問ImageView和TextView。 但問題是android無法找到它,並且它保持為空,我無法使用它。

當我嘗試到達它時,它拋出一個空異常:(imageview.set ...)。

這是我的arrayadapter的類代碼:

    package ***.***.***;

import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by yoav on 09/08/15.
 */
public class ItemAdapter extends ArrayAdapter<Item> {

    // declaring our ArrayList of items
    private ArrayList<Item> cards;
    Context mContext;
    /* here we must override the constructor for ArrayAdapter
    * the only variable we care about now is ArrayList<Item> objects,
    * because it is the list of objects we want to display.
    */
    public ItemAdapter(Context context, int textViewResourceId, int textV, ArrayList<Item> newCards) {
        super(context, textV, textViewResourceId, newCards);
        this.cards = newCards;
        mContext = context;
    }

    /*
     * we are overriding the getView method here - this is what defines how each
     * list item will look.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        View v = convertView;

        ViewHolder holder = new ViewHolder();

        // First let's verify the convertView is not null
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item, parent, false);
            // Now we can fill the layout with the right values
            TextView tv = (TextView) v.findViewById(R.id.tNameee);
            ImageView img = (ImageView) v.findViewById(R.id.ivUserImage);

            holder.txtTitle = tv;
            holder.imageView = img;


            v.setTag(holder);
        }
        else
            holder = (ViewHolder) v.getTag();

        System.out.println("Position [" + position + "]");
        Item p = cards.get(position);
        holder.txtTitle.setText(p.getName());
        holder.imageView.setImageResource(R.drawable.******);



        return v;
    }

    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
    }
}

我的XML文件item.xml:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_gravity="bottom|center_horizontal"
    android:layout_marginBottom="15dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tNameee"
        android:textSize="40sp"
        android:textColor="@android:color/white"
        android:background="@drawable/polaroid"
        android:gravity="center"
        tools:text="@string/hello_world"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:layout_width="282dp"
        android:layout_height="255dp"
        android:layout_margin="10dp"
        android:background="#1234ff"
        android:id="@+id/ivUserImage"/>

    <View
        android:id="@+id/item_swipe_left_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"

        android:layout_margin="10dp"
        android:background="@drawable/polaroid" />

    <View
        android:id="@+id/item_swipe_right_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"

        android:layout_margin="10dp"
        android:layout_gravity="right"
        android:background="@drawable/polaroid" />

</FrameLayout>

有人知道我做錯了什么嗎?

item.xml中有一個名為“ tTexteeee”的對象。 並且有一個名為“ ivUserImage.xml”的對象。

這可能無濟於事,但是嘗試直接分配視圖:

@Override
public View getView(int position, View convertView, ViewGroup parent){

    View v = convertView;

    ViewHolder holder = new ViewHolder();

    // First let's verify the convertView is not null
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.item, parent, false);
        // Now we can fill the layout with the right values
        holder.tv = (TextView) v.findViewById(R.id.tNameee);
        holder.img = (ImageView) v.findViewById(R.id.ivUserImage);

        v.setTag(holder);
    }
    else
        holder = (ViewHolder) v.getTag();

    System.out.println("Position [" + position + "]");
    Item p = cards.get(position);
    holder.tv.setText(p.getName());
    holder.img.setImageResource(R.drawable.******);



    return v;
}

private class ViewHolder {
    ImageView tv;
    TextView img;
}

如果您需要更多幫助,請發布xml文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM