gpt4 book ai didi

android - 如何让 ListPopupWindow 与自定义适配器一起使用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:16 33 4
gpt4 key购买 nike

我想在单击按钮时显示的 ListPopupWindow 中显示一个简单的字符串数组。然而,我遇到了问题,因为当我对 ArrayAdapter<String> 进行一些最小设置时或自定义适配器,当我去显示弹出窗口时遇到资源未找到异常。这是我正在使用的代码(后面是堆栈跟踪)。关于正在发生的事情有什么想法吗?

public class AndroidSandboxActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showListPopup(btn);
}
});
}

public void showListPopup(View anchor) {
ListPopupWindow popup = new ListPopupWindow(this);
popup.setAnchorView(anchor);

ListAdapter adapter = new MyAdapter(this);
popup.setAdapter(adapter);
popup.show();
}

public static class MyAdapter extends BaseAdapter implements ListAdapter {
private final String[] list = new String[] {"one","two","three"};
private Activity activity;
public MyAdapter(Activity activity) {
this.activity = activity;
}

@Override
public int getCount() {
return list.length;
}

@Override
public Object getItem(int position) {
return list[position];
}

@Override
public long getItemId(int position) {
return position;
}

private static int textid = 1234;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = null;
if (convertView == null) {
LinearLayout layout = new LinearLayout(activity);
layout.setOrientation(LinearLayout.HORIZONTAL);

text = new TextView(activity);
text.setId(textid);
layout.addView(text);
convertView = layout;
} else {
text = (TextView)convertView.findViewById(textid);
}
text.setText(list[position]);
return convertView;
}
}
}

这是堆栈跟踪(令我感到困惑的是,当我使用自己的自定义适配器时,它说它正在使用 ArrayAdapter):

Thread [<1> main] (Suspended (exception Resources$NotFoundException))   
Resources.loadXmlResourceParser(int, String) line: 2047
Resources.getLayout(int) line: 853
PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup, boolean) line: 389
ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 375
ArrayAdapter.getView(int, View, ViewGroup) line: 366
ListPopupWindow$DropDownListView(AbsListView).obtainView(int, boolean[]) line: 2146
ListPopupWindow$DropDownListView.obtainView(int, boolean[]) line: 1156
ListPopupWindow$DropDownListView(ListView).measureHeightOfChildren(int, int, int, int, int) line: 1261
ListPopupWindow.buildDropDown() line: 1083
ListPopupWindow.show() line: 517
AndroidSandboxActivity.showListPopup() line: 41
AndroidSandboxActivity$1.onClick(View) line: 28
Button(View).performClick() line: 3122
View$PerformClick.run() line: 11942
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 132
ActivityThread.main(String[]) line: 4028
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 491
ZygoteInit$MethodAndArgsCaller.run() line: 844
ZygoteInit.main(String[]) line: 602
NativeStart.main(String[]) line: not available [native method]

如有任何帮助,我们将不胜感激!

最佳答案

我弄清楚了为什么 ArrayAdapter 不起作用;我为数组适配器选择了一个不正确的资源 ID,因为我不完全理解 ArrayAdapter 在 ListView 方面的工作原理。使用这个内置的 android 资源:

android.R.layout.simple_dropdown_item_1line

在构建数组适配器时的相关资源参数中,我能够让事情正常进行。不过,我不确定为什么上面的自定义适配器不起作用,因为上面代码中的自定义适配器没有引用任何特定的 ID。我提供的堆栈跟踪可能来自使用了 ArrayAdapter 的旧版本代码。

关于android - 如何让 ListPopupWindow 与自定义适配器一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986266/

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