简体   繁体   中英

Android Studio ArrayAdapter requires the resource ID to be a TextView

I am new to android programming and I have a problem. This is my first question on stackoverflow so I hope I am not doing a mistake about this.

   public class CustomAdapter extends ArrayAdapter<Loger> {

        Context context;
        private ArrayList<Loger> logs;
        private LayoutInflater mInflater;
        private boolean mNotifyOnChange = true;
        private URL url;
        private Bitmap bitmap;
        private Drawable drawable;


        public CustomAdapter(Context context, ArrayList<Loger> listLoger) {
            super(context, R.layout.liesviewlayout);
            this.context = context;
            this.logs = new ArrayList<Loger>(listLoger);
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return logs.size();
        }

        @Override
        public Loger getItem(int position) {
            return logs.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public int getPosition(Loger item) {
            return logs.indexOf(item);
        }

        @Override
        public int getViewTypeCount() {
            return 1; //Number of types + 1 !!!!!!!!
        }

        @Override
        public int getItemViewType(int position) {
            return 1;
        }


        public View getView(final int position, View convertView, ViewGroup parent, final Context context) {
            final ViewHolder holder;
            int type = getItemViewType(position);

            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                    case 1:

                        convertView = mInflater.inflate(R.layout.liesviewlayout,parent, false);

                        holder.name = (TextView) convertView.findViewById(R.id.name);
                        holder.comment = (TextView) convertView.findViewById(R.id.comment);
                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Thread thread = new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        url = new URL(logs.get(position).getImageUrl());
                        Log.d("MyTag", url.toString());
                        InputStream inputstream_ = (InputStream) new URL(logs.get(position).getImageUrl()).getContent();
                        //bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                        drawable = Drawable.createFromStream(inputstream_, "src");

                        //holder.icon.setImageDrawable(drawable);
                        holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ned_flanders2));

                        holder.name.setText(logs.get(position).getFirstname() );
                        //+ logs.get(position).getLastname() + logs.get(position).getLocation() + logs.get(position).getTime() + logs.get(position).getStatus());
                        holder.comment.setText(logs.get(position).getComment());
                        holder.pos = position;
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });

            thread.start();

            return convertView;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            mNotifyOnChange = true;
        }

        public void setNotifyOnChange(boolean notifyOnChange) {
            mNotifyOnChange = notifyOnChange;
        }


        //---------------static views for each row-----------//
        static class ViewHolder {

            TextView name;
            TextView comment;
            ImageView icon;
            int pos; //to store the position of the item within the list
        }
}

And my layout class:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"

    android:padding="6dip">

    <ImageView

        android:id="@+id/simge"
        android:layout_width="75dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="6dip"
        android:src="@drawable/pusulalogo" />

    <TextView
        android:id="@+id/comment"
        android:layout_width="500dp"
        android:layout_height="30dip"
        android:layout_toRightOf="@id/simge"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="false"
        android:singleLine="true"
         />

    <TextView
        android:id="@+id/name"
        android:layout_width="500dp"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/simge"
        android:layout_alignParentRight="false"
        android:layout_alignParentTop="true"
        android:layout_above="@id/comment"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical" />

</RelativeLayout>

in my main activity i populate my ArrayList and then

 lv = (ListView) findViewById(R.id.listView);

        this.mAdapter = new CustomAdapter(this, listLoger);
        lv.setAdapter(mAdapter);

my Loger class

public class Loger {
    public String ImageUrl;
    public String Firstname;
    public String Lastname;
    public String Location;
    public String Time;
    public String Status;
    public String Comment;

    public String getComment() {
        return Comment;
    }

    public void setComment(String comment) {
        Comment = comment;
    }

    public Loger(String imageUrl, String location, String lastname, String firstname, String time, String status, String comment) {
        ImageUrl = imageUrl;
        Location = location;
        Lastname = lastname;
        Firstname = firstname;
        Time = time;
        Status = status;
        Comment = comment;
    }


    public String getImageUrl() {
        return ImageUrl;
    }

    public void setImageUrl(String imageUrl) {
        ImageUrl = imageUrl;
    }

    public String getFirstname() {
        return Firstname;
    }

    public void setFirstname(String firstname) {
        Firstname = firstname;
    }

    public String getLastname() {
        return Lastname;
    }

    public void setLastname(String lastname) {
        Lastname = lastname;
    }

    public String getLocation() {
        return Location;
    }

    public void setLocation(String location) {
        Location = location;
    }

    public String getTime() {
        return Time;
    }

    public void setTime(String time) {
        Time = time;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

i get the following crash

PID:2370 ArrayAdapter requires the resource ID to be a TextView Caused android.widget.RelativeLayout cannot be cast to android.widget.TextView

The exception is pretty explanatory by itself, but it's probably better to have a bit of knowledge about the internals to better understand the issue.

The official ArrayAdapter documentation is always a good place to start with. You've chosen the first constructor , with the following note:

int resource : The resource ID for a layout file containing a TextView to use when instantiating views.

As mentioned, the constructor is assuming that the layout only contains one TextView , which is not the case for your layout. However, a short look at the documentation will lead you to the following constructor :

ArrayAdapter(Context context, int resource, int textViewResourceId)

try: in main activity pass the id of the layout xml while initialising your array adapter ie this.mAdapter = new CustomAdapter(this, R.id., listLoger);

Also check your custom adapter class, text view and image view havenot been initialised.

Ok i deleted my customadapter class and rewrited it. You were right about resid. I am adding this in case of anybody needs it.

public class CustomAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<Loger> logs;
private static LayoutInflater inflater=null;
public Resources res;
Loger tempValues=null;
int i=0;
private URL url;


public CustomAdapter(Activity a, ArrayList<Loger> listLoger,Resources resLocal) {

    activity = a;
    logs=listLoger;
    res = resLocal;

    inflater = ( LayoutInflater )activity.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {

    if(logs.size()<=0)
        return 1;
    return logs.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{

    public TextView text;
    public TextView text1;
    public TextView textWide;
    public ImageView image;

}

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

    View vi = convertView;
    ViewHolder holder;

    if(convertView==null){

        vi = inflater.inflate(R.layout.liesviewlayout, null);

        holder = new ViewHolder();
        holder.text = (TextView) vi.findViewById(R.id.name);
        holder.text1=(TextView)vi.findViewById(R.id.comment);
        holder.image=(ImageView)vi.findViewById(R.id.image);

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

    if(logs.size()<=0)
    {
        holder.text.setText("No Data");

    }
    else
    {

        tempValues=null;
        tempValues = ( Loger ) logs.get( position );



        holder.text.setText( tempValues.getFirstname() );
        holder.text1.setText( tempValues.getImageUrl() );
        holder.image.setImageResource(
                res.getIdentifier(
                        "com.androidexample.customlistview:drawable/"+tempValues.getImageUrl()
                        ,null,null));

    }
    return vi;
}

}

and in main activity

Resources res =getResources();
        lv = ( ListView )findViewById( R.id.listView ); 

        //Create Custom Adapter
        mAdapter=new CustomAdapter( this, listLoger,res );
        lv.setAdapter( mAdapter );

Thanks for your answers guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM