[英]Custom ArrayAdapter getView Not Being Called--Why Not?
我在另一個Activity類中使用了相同的模式,它完美地運行。 但是在這個類(也是一個Activity)中, 從不調用getView。 我通過記錄fbFriendMatchAdapter.getCount()確認適配器中有9個項目。 我已經嘗試將數據源更改為String []變量,但這對getView的(缺少)沒有影響。
我很感激任何建議! 我已經徹底研究過了。 這個問題有很多種,但都沒有解決我的問題。 這就是我發布新問題的原因。
//Here is the ArrayList data source definition.
//It is loaded using a for loop with 9 values.
private List<String> fbFriendMatchAdapter=new ArrayList<String>();
// Here is the Custom Array Adapter
ArrayAdapter<String> fbFriendMatchAdapter = new ArrayAdapter<String>(this,
R.layout.row_left, R.id.ListViewName, fbFriendPotentialMatchArrayList) {
@Override
public View getView(final int dialogPosition, View convertView, ViewGroup listParent) {
LayoutInflater inflater = getLayoutInflater();
View fbFriendMatchDialogViewRow = inflater.inflate(R.layout.row_left, listParent, false);
Log.d(BBTAG, String.format("BBSetup getView[%s] name=%s", dialogPosition, buddyDisplayName ));
return fbFriendMatchDialogViewRow;
} // [END getView]
@Override
public String getItem(int position) {
return fbFriendPotentialMatchArrayList.get(position);
}
};
//Here is the dialog that includes the ArrayAdapter:
AlertDialog fbFriendsMatchDialog = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.PetesSpinnerReplacement))
.setTitle("Select Correct Facebook Friend") //FYI overridden by custom title
.setAdapter(fbFriendMatchAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedFBFriend) {
buddyFBUIDs[buddyNumber] = fbFriendUIDsList.get(selectedFBFriend);
}
})
.setMessage("No matches found")
.setCancelable(true)
.setPositiveButton("Set as Facebook Friend", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int iFriend) {
dialog.dismiss();
}
})
.setNegativeButton("Friend Not Listed", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
buddyFBUIDs[buddyNumber] = "n/a"; //record lack of FB uid
}
})
.create(); //build dialog
fbFriendsMatchDialog.show(); // now show dialog
我建議不要試圖為此內聯一個ArrayAdapter
子類,因為我相信ArrayAdapter
對你的布局和數據結構做了一些假設,可能會讓你失望。 編寫自己的自定義適配器非常簡單,我幾乎建議不要使用ArrayAdapter
(除了最簡單的用例)。 只需子類化BaseAdapter
和四個必需的方法,然后使用它。 像這樣的東西:
private class MatchAdapter extends BaseAdapter {
private List<String> mItems;
private LayoutInflater mInflater;
public MatchAdapter (Context c, List<String> items) {
mItems = items;
//Cache a reference to avoid looking it up on every getView() call
mInflater = LayoutInflater.from(c);
}
@Override
public int getCount () {
return mItems.size();
}
@Override
public long getItemId (int position) {
return position;
}
@Override
public Object getItem (int position) {
return mItems.get(position);
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
//If there's no recycled view, inflate one and tag each of the views
//you'll want to modify later
if (convertView == null) {
convertView = mInflater.inflate (R.layout.row_left, parent, false);
//This assumes layout/row_left.xml includes a TextView with an id of "textview"
convertView.setTag (R.id.textview, convertView.findViewById(R.id.textview));
}
//Retrieve the tagged view, get the item for that position, and
//update the text
TextView textView = (TextView) convertView.getTag(R.id.textview);
String textItem = (String) getItem(position);
textView.setText(textItem);
return convertView;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.