2014-10-10 68 views
0

我已經問了一個類似的問題here和一個用戶建議子類ArrayAdapter,但我有問題。 這裏是我的問題:Android中的子類ArrayAdapter顯示ArrayList

我使用HashMap<String, String>SimpleAdapter對象在我ListView顯示的用戶數據,但現在我想用含有真正的用戶對象的ArrayList<User>();更換HashMap<String, String>

這裏是舊代碼:

HashMap<String, String> map = new HashMap<String, String>(); 

       map.put(TAG_USERNAME, c.getString(TAG_USERNAME)); 
       map.put(TAG_FIRSTNAME, c.getString(TAG_FIRSTNAME)); 
       map.put(TAG_LASTNAME, c.getString(TAG_LASTNAME)); 
       map.put(TAG_ADDRESS, c.getString(TAG_ADDRESS)); 
       usersList.add(map); 

[...]現在

ListAdapter adapter = new SimpleAdapter(this, usersList, 
        R.layout.single_user, new String[] { TAG_USERNAME, 
          TAG_FIRSTNAME, TAG_LASTNAME, TAG_ADDRESS }, 
        new int[] { R.id.textViewUsername, R.id.textViewFirstName, 
          R.id.textViewLastName, R.id.textViewAddress }); 

,作爲建議,我想繼承ArrayAdapter,但我沒有計劃如何getView(int, View, ViewGroup)應該看喜歡,達到我的目標。

這裏是我當前的代碼:

public class UserArrayAdapter extends ArrayAdapter<User> { 

    private Context mcon; 
    private List<User> userList; 

    public UserArrayAdapter(Context context, int resource, 
      List<User> userList) { 
     super(context, resource); 
     mcon = context; 
     this.userList = userList; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     for (User usr : userList) { 
      findViewById(R.id.textViewUsername).setText(usr.getFirstName()); 
     } 
     return super.getView(position, convertView, parent); 
    } 

} 

下面是用戶等級:

public class User { 

    private String username, firstName, lastName, address; 

    public User(String username, String firstName, String lastName, 
      String address) { 
     setUsername(username); 
     setFirstName(firstName); 
     setLastName(lastName); 
     setAddress(address); 
    } 
    public String getUsername() { 
     return username; 
    } 
    public void setUsername(String username) { 
     if (username == null) { 
      throw new IllegalArgumentException("username is null."); 
     } 
     this.username = username; 
    } 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     if (firstName == null) { 
      throw new IllegalArgumentException("firstName is null."); 
     } 
     this.firstName = firstName; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     if (lastName == null) { 
      throw new IllegalArgumentException("lastName is null."); 
     } 
     this.lastName = lastName; 
    } 
    public String getAddress() { 
     return address; 
    } 
    public void setAddress(String address) { 
     if (address == null) { 
      throw new IllegalArgumentException("address is null."); 
     } 
     this.address = address; 
    } 
} 

而且我以爲我會這樣稱呼它:

ListAdapter ad = new UserArrayAdapter(this, R.layout.single_user, usersArrList); 

任何幫助將不勝感激。

+0

向我們展示您的用戶類 – 2014-10-10 16:04:19

+0

當然。編輯完成。 – user3475602 2014-10-10 16:07:20

回答

2

我建議繞過ArrayAdapter共載您的適配器,因爲你會做定製工作,您將在內部重複由適配器完成的工作。無論如何,實施是微不足道的。只是子類BaseAdapter

public class UserAdapter extends BaseAdapter { 
    private final List<User> mUsers; 
    private final LayoutInflater mInflater; 

    public UserAdapter(Context ctx, Collection<User> users) { 
     mUsers = new ArrayList<User>(); 
     mInflater = LayoutInflater.from(ctx); 
     if (users != null) { 
      mUsers.addAll(users); 
     } 
    } 

    @Override 
    public int getCount() { 
     // Just return the number of users in your data set 
     return mUsers.size(); 
    } 

    @Override 
    public long getItemId(int pos) { 
     // Mostly irrelevant if you're not using Cursors 
     return pos; 
    } 

    @Override 
    public User getItem(int pos) { 
     // Just return the item at the specified position 
     return mUsers.get(pos); 
    } 

    @Override 
    public View getView(int pos, View convertView, ViewGroup parent) { 
     UserViewHolder uvh; 

     if (convertView == null) { 
      // Only inflate the layout if there's not already a recycled view 
      convertView = mInflater.inflate(R.layout.single_user, parent, false); 

      // Tag the view with a class holding the views found with 
      // findViewById() to prevent lookups later 
      uvh = new UserViewHolder(convertView); 
      convertView.setTag(uvh); 
     } else { 
      // If the view is non-null, then you will have already 
      // created the view holder and set it as a tag 
      uvh = (UserViewHolder) convertView.getTag(); 
     } 

     // Now just get the user at the specified position and 
     // set up the view as necessary 
     User user = getItem(pos); 
     uvh.usernameTxt.setText(user.getUsername()); 

     return convertView; 
    } 

    public static class UserViewHolder { 
     public final TextView usernameTxt; 

     public UserViewHolder(View v) { 
      usernameTxt = (TextView) v.findViewById(R.id.textViewUsername); 
     } 
    } 
} 
+1

謝謝你,先生!這解決了這個問題。祝你今天愉快!!! – user3475602 2014-10-10 16:27:46

+0

不客氣:)你也是! – kcoppock 2014-10-10 16:28:08

0

這樣做:

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    // Getting the view to use (letting ArrayAdapter do this job) 
    View v = super.getView(position, convertView, parent); 

    // Getting the current user 
    User usr = userList.get(position); 

    // Filling the firstname in the textview 
    ((TextView) v.findViewById(R.id.textViewUsername)).setText(usr.getFirstName()); 

    return v; 
} 

然後,在ListView

ListAdapter ad = new UserArrayAdapter(this, R.layout.single_user, usersArrList); 
listView.setAdapter(ad); 
+0

謝謝你的幫助。我完全按照建議做的,在我的LogCat中沒有錯誤,但我在應用程序中看不到任何用戶。任何想法如何調試? – user3475602 2014-10-10 16:23:43

+1

你可以用活動代碼編輯你的第一篇文章,這個代碼是關於listView/adapter – ToYonos 2014-10-10 16:25:31

+0

的活動代碼謝謝你的幫助,但是我用kcoppock的解決方案解決了它。 – user3475602 2014-10-10 16:28:22