2016-03-26 234 views
0

嘗試使用片段中的數據獲取列表視圖。 通過我知道的代碼我應該工作,但事實並非如此。 Als試圖使用ListFragment而不是使用Fragment和BaseAdapter而不是ArrayAdapter,但兩者都不起作用。包含ListView的Android片段

片段

public class ContactsFragment extends Fragment { 
    private DashboardActivity dashboard; 
    private List<User> contacts; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_contacts, container, false); 
    ... 
    ... 
    ListView contactsListView = (ListView)rootView.findViewById(R.id.contacts_list_view); 

    ContactsListAdapter contactsListAdapter = new ContactsListAdapter(getActivity(), contacts); 

    contactsListView.setAdapter(contactsListAdapter); 

    return rootView; 
} 

適配器

public class ContactsListAdapter extends ArrayAdapter<User> { 

    private Context context; 
    private List<User> contacts; 

    public ContactsListAdapter(Context context, List<User> contacts) { 
     super(context, R.layout.contact_list_item); 

     this.context = context; 
     this.contacts = contacts; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup viewGroup) { 
    User contact = contacts.get(position); 

    View contactView = convertView; 

    if (contactView == null) { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     contactView = inflater.inflate(R.layout.contact_list_item, null); 
    } 

    TextView contactEmail = (TextView)contactView.findViewById(R.id.contact_item_email); 
    contactEmail.setText(contact.getEmail()); 

    TextView contactPhone = (TextView)contactView.findViewById(R.id.contact_item_phone); 
    contactPhone.setText(contact.getPhone()); 

    return contactView; 
} 

我錯過了財產以後?也許使用其他基類? 如果我的問題不清楚,這裏沒有足夠的信息幫助,請告訴我!

在此先感謝

回答

1

試試這個:

public ContactsListAdapter(Context context, List<User> contacts) { 
    super(context, R.layout.contact_list_item,contacts); //change here 
    this.context = context; 
    this.contacts = contacts; 
} 
+0

嗷的人,我很愚蠢。只是找不到問題。 非常感謝幫助我在這裏! –