ImageView在android中的自定义ArrayList适配器中保持NULL

时间:2015-08-09 12:36:19

标签: java android xml android-arrayadapter

我正在尝试在自定义ArrayAdapter中访问我的ImageView和TextView。但问题是android无法找到它并且它保持为null并且我无法使用它。

当我尝试访问它时抛出一个空异常:(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”的对象。

1 个答案:

答案 0 :(得分:0)

这可能不会有所帮助,但请尝试直接分配视图:

@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文件。