gpt4 book ai didi

android - 自定义 ArrayAdapter getView 未被调用——为什么不呢?

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:47 31 4
gpt4 key购买 nike

我在另一个 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;
}
}

关于android - 自定义 ArrayAdapter getView 未被调用——为什么不呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17397605/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com