2014-04-04 56 views
1

它可能會很簡單的答案,但請看看這個錯誤。我想讓適配器返回一個View。我一直在尋找網絡中的任何地方,但仍然無法使其工作。請幫幫我。適配器錯誤Android

這裏是我的代碼:

MAIN ACTIVITY shortly: 

public void setAdapter() 
    { 
     adapter = new ListAdapter(this, R.id.list_item, list); 

    } 

AND ADAPTER: 

public class ListAdapter extends ArrayAdapter<GifModel> { 


    private ArrayList<GifModel> objects; 
    private Context context; 


    public ListAdapter(Context context, int textViewResourceId, ArrayList<GifModel> objects) { 
     super(context, textViewResourceId, objects); 
     this.objects = objects; 
     this.context = context; 
    } 


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

     View v = convertView; 


     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      // HERE IS A CRITICAL ERROR. IT CAN NOT INFLATE THIS LAYOUT AND DO NOT KNOW WHY 
      v = inflater.inflate(R.layout.newListItem, null); 
     } 


     GifModel i = objects.get(position); 

     if (i != null) { 


      TextView tt = (TextView) v.findViewById(R.id.nameOfTheItem); 
      TextView ttd = (TextView) v.findViewById(R.id.dateOfAdded); 


      if (tt != null){ 
       tt.setText(i.getNameOfIteme()); 
      } 
      if (ttd != null){ 
       ttd.setText(i.getAddedDate()); 
      } 

     } 

     // the view must be returned to our activity 
     return v; 

    } 

END LIST ITEM VIEW: 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    android:background="@android:color/darker_gray" 
    android:id="@+id/newListItem" > 

    <ImageView 
     android:id="@+id/listImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="19dp" 
     android:layout_marginTop="16dp" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/dateOfAdded" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/listImageView" 
     android:layout_marginLeft="26dp" 
     android:layout_toRightOf="@+id/listImageView" 
     android:text="HAHA" /> 

    <TextView 
     android:id="@+id/nameOfTheItem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/dateOfAdded" 
     android:layout_alignTop="@+id/listImageView" 
     android:text="UMCUMC" /> 

    <ImageView 
     android:id="@+id/ratingView" 
     android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/nameOfTheItem" 
     android:layout_marginRight="10dp" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 
+0

你需要改變'newListItem'的名稱爲小寫的'blackbelt'說 –

+0

我已經做到了,並沒有什麼新的改變。 – Darek

回答

2

你不能佈局文件使用駱駝套管名。只允許字符是a-z,0-9,_

+0

我的佈局文件被命名爲:list_item.xml。如果這個錯誤不會消失,我不能前進。 – Darek

+0

嘗試清理並重建然後 – Blackbelt

+0

是的,我也完成了,它仍然存在;/ – Darek

0

嘗試用

((Activity) context).getLayoutInflater(); 
+0

這不是問題,這段代碼很好 –

+0

我試過這個,但是它仍然給我顯示錯誤 – Darek

0

您是否嘗試過使用這種吹氣等而不是替換

(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

inflater.inflate(R.layout.newListItem, parent, false); 

我有類似的問題,但這解決了它。

+0

是的,但是我仍然有錯誤 – Darek

+0

也許你的layoutinflater爲空?嘗試其他構造函數,如 'final LayoutInflater inflater = LayoutInflater.from(context);'。此外,您可以嘗試發佈一些日誌消息(或最簡單的System.out.println();查看您使用的值是什麼。 – mimetist

1

在適配器的構造函數中聲明並初始化您的LayoutInflater也是一個好主意,getView會在不可預知的時間被調用並且每次聲明對象時都會導致大量內存開銷。

我通常設置我的適配器是這樣的:適配器的內部

public MyAdapter extends BaseAdapter { 

    private LayoutInflater layoutInflater; 

    public MyAdapter(Context context) { 
     layoutInflater = (LayoutInflater) context.getSystemsService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    // getId,etc... 
} 
+0

當然。 – Darek

0

附加接口ViewHolder。你可以看看如何去做。 和你的錯誤,這應該工作:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.YourAdapterLayoutt, parent, false);