2014-04-04 4 views
1

아마도 이것은 간단한 대답 일 것이지만이 오류를 살펴보십시오. 보기를 반환하는 어댑터를 만들고 싶습니다. 나는 네트워크에서 도처에서 찾고 있었지만 여전히 작동하게 만들 수는 없다. 도와주세요.안드로이드 안드로이드에있는 오류

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

하면 레이아웃 파일 낙타 맡았다 이름을 사용할 수 없습니다 :

여기 내 코드입니다. 유일하게 허용되는 문자는

+0

내 레이아웃 파일의 이름은 list_item.xml입니다. 이 오류가 사라지지 않으면 앞으로 나아갈 수 없습니다. – Darek

+0

청소하고 다시 작성하려고 시도하십시오 – Blackbelt

+0

예 이미 완료했으나 여전히 남아 있습니다/ – Darek

0

는 대신 다른 인플레이터를 사용하려고 적이

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

이것은 문제가되지 않습니다,이 코드는 괜찮습니다 –

+0

나는 이것을 시도했지만 여전히 오류가 표시됩니다 – Darek

0

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

를 교체하십시오 a-z,0-9,_입니까?

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

나는 이와 비슷한 문제가 있었지만 해결되었다.

+0

그래,하지만 여전히 오류있어 – Darek

+0

아마도 layoutinflater가 null입니까? '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); 
관련 문제