大家好! 这可能和派一样容易,但是说实话,我已经苦苦挣扎了大约1.5个小时。
我想要实现的是通过AdapterView.OnItemClickListener中的id来区分ListPopupWindow(用作微调器的替代品),但是由于以下原因,我无法做到这一点:
不可转换的类型,无法将'android.view.View'强制转换为'android.Widget.ListPopupWindow'
in line:ListPopupWindow listPopupWindow = (ListPopupWindow)父级;
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListPopupWindow listPopupWindow = (ListPopupWindow) parent;
int lpwID = listPopupWindow.getId();
switch (lpwID) {
...
}
}
};
我的其余代码如下:
//text views, ListPopupWindows & anchors, ArrayLists
tvCategory = findViewById(R.id.tvCategory_activityEvent);
tvSubcategory = findViewById(R.id.tvSubcategory_activityEvent);
lpwCategory = new ListPopupWindow(this);
lpwSubcategory = new ListPopupWindow(this);
lpwCategory.setAnchorView(tvCategory);
lpwSubcategory.setAnchorView(tvsubcategory);
//arrayAdapters
lCategories = DBManager.getDbInstance(Event.this).serviceCategoryDAO().getCategories();
aaCategory = new ArrayAdapter<>(Event.this, R.layout. spinner_item , lCategories);
aaSubcategory = new ArrayAdapter<>(Event.this, R.layout.spinner_item);
aaCategory.setDropDownViewResource(android.R.layout. simple_spinner_dropdown_item );
lpwCategory.setAdapter(aaCategory);
aSubcategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
lpwSubcategory.setAdapter(aaSubcategory);
lpwCategory.setOnItemClickListener(onItemClickListener);
lpwSubcategory.setOnItemClickListener(onItemClickListener);
另一个问题是:如何将ID分配给ListPopupWidows?说到微调器,它很简单:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
spinnerId = spinner.getId
...}
任何提示,技巧和窍门都将不胜感激,因为以下几个原因,我想远离微调:)谢谢!!