簡體   English   中英

Android Studio ArrayAdapter要求資源ID為TextView

[英]Android Studio ArrayAdapter requires the resource ID to be a TextView

我是android編程的新手,但是有問題。 這是我關於stackoverflow的第一個問題,所以我希望我對此沒有做錯。

   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
        }
}

和我的布局類:

<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>

在我的主要活動中,我填充了ArrayList,然后

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

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

我的Loger班

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;
    }

我得到以下崩潰

PID:2370 ArrayAdapter要求資源ID為TextView。導致android.widget.RelativeLayout無法轉換為android.widget.TextView

異常本身是可以解釋的,但是最好對內部有一點了解以更好地理解問題。

正式的ArrayAdapter文檔始終是一個不錯的起點。 您選擇了第一個構造函數 ,並注意以下幾點:

int resource :包含要在實例化視圖時使用的TextView的布局文件的資源ID。

如前所述,構造函數假定布局僅包含一個TextView ,而布局不是這種情況。 但是,對文檔的簡短瀏覽將帶您進入以下構造函數

ArrayAdapter(Context context, int resource, int textViewResourceId)

嘗試:在主要活動中,在初始化數組適配器時傳遞布局xml的ID,即this.mAdapter = new CustomAdapter(this,R.id.,listLoger);

還要檢查您的自定義適配器類,文本視圖和圖像視圖尚未初始化。

好的,我刪除了我的customadapter類並重寫了它。 你對渣油是正確的。 如果有人需要,我會添加它。

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;
}

}

在主要活動中

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

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

謝謝你們的回答。

暫無
暫無

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

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