2016-08-18 97 views
0

我是android编程的新手,我遇到了问题。这是我在stackoverflow上的第一个问题,所以我希望我不会对此做出错误。Android Studio ArrayAdapter需要资源ID是TextView

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引起android.widget.RelativeLayout不能转换到android.widget.TextView

回答

1

尝试:在主要活动中通过布局XML的ID,同时初始化您的阵列适配器即this.mAdapter =新的CustomAdapter(this,R.id.,listLoger);

还检查您的自定义适配器类,文本视图和图像视图havenot被初始化。

+0

@ TR4Android 我改变了我的代码,但它没有任何区别。仍然得到相同的错误。 'this.mAdapter = new CustomAdapter(this,listLoger,R.id.name); lv =(ListView)findViewById(R.id.listView); 如果(lv!=空){llllllllllll。setAdapter(mAdapter); }' – Balik

1

除外TextView的是本身很说明,但它可能是最好有关于内部的一些知识以更好地理解问题。

官方ArrayAdapter文档始终是一个很好的开始。你所选择的first constructor,有以下注意事项:

int resource:包含一个TextView实例化视图时使用布局文件中的资源ID。

如前所述,构造函数假定布局仅包含一个TextView,而布局并非如此。不过,短期看,在文档将带领您到following constructor

ArrayAdapter(Context context, int resource, int textViewResourceId)

0

好的,我删除了我的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); 

谢谢您的回答家伙。

相关问题