2014-07-26 125 views
0

我正在使用getView()方法沒有被調用的自定義適配器。我有一個回調接口方法,當異步任務完成時它會被調用。ArrayAdapter的行爲很糟糕

public class Student extends Activity implements AsyncFinishInterface 
    { 

     private ListView list; 

      @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.student_screen); 

      list= (ListView) findViewById(R.id.listViewList); 
      new Aynctask().execute(); 

     } 
@Override 
    public void finish(String response, String method) throws Exception { 

     ArrayList<Friend> FriendList= new ArrayList<Friend>(); 
     Friend friend; 
     JSONObject row; 
     JSONObject json= new JSONObject(response); 
     JSONArray jsonArray=json.getJSONArray("Friends"); 
     int size=jsonArray.length(); 
     Friend[] friendArray= new Friend[size]; 
     for(int i=0 ; i<jsonArray.length(); i++) 
     {   
      friend = new Friend(); 
      row= jsonArray.getJSONObject(i); 

      friend.setFirstName(row.getString("firstName")); 
      friend.setLastName(row.getString("lastName")); 
      friend.setEmail(row.getString("email")); 

      friendArray[i]=friend; 
         } 
ContentAdapter contentAdapter= new ContentAdapter(this, R.layout.friend_detail, friendArray); 
     list.setAdapter(contentAdapter);  //Till here it's working fine. 
    } 



class ContentAdapter extends ArrayAdapter<Friend> { 
     Context context; 
     int layoutResourceId; 
     Friend data[] = null; 

     public ContentAdapter(Context context, int resource, Friend[] objects) { 
      super(context, resource, objects); 
      this.layoutResourceId = resource; 
      this.data = objects; 
      this.context = context; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      //   ContentsHolder holder; 
      LayoutInflater inflater = ((Home_Screen) context) 
        .getLayoutInflater(); 
      return row = inflater.inflate(layoutResourceId, parent, false); 
     } 


    } 

有沒有人有想法這裏有什麼問題。

<?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="match_parent" 
    android:background="@android:color/white" > 

    <ImageView 
     android:id="@+id/imageViewFrndImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="14dp" 
     android:src="@drawable/app_icon" /> 

    <TextView 
     android:id="@+id/textViewFrndName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textViewFrndEmail" 
     android:layout_alignTop="@+id/imageViewFrndImage" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/textViewFrndEmail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textViewFrndName" 
     android:layout_toRightOf="@+id/imageViewFrndImage" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</RelativeLayout> 
+0

int i = super.getCount(); return i;你應該返回data.length –

+0

調試你的代碼並檢查Adapter的構造函數上的'objects'的大小,在ArrayAdapter中你不需要'getCount',刪除那個關於getcount()的 –

+0

它甚至不被調用 – Sunny

回答

0

你的代碼應該是代替:

LayoutInflater inflater = ((Home_Screen) context) 
       .getLayoutInflater(); 

試試這個:

LayoutInflater inflater =LayoutInflater.from(context); 

看看它是否工作!

+0

'ArrayAdapter'類不需要'getCount',需要'BaseAdapter' –

+0

編輯,我很困惑。謝謝 ! – Leonardo

相關問題