描述
我花了大约一个小时试图找出一个numpy数组构造错误。我一定不能正确使用numpy dtypes,但错误信息不够描述,我没有给出足够好的堆栈跟踪来查找错误。
创建相同错误的简化示例:
public class CustomAdapter extends BaseAdapter {
Context context;
List<Object> objectList;
public boolean clicked=false;
public CustomAdapter(Context context, List<Object> objectList) {
this.context = context;
this.objectList = objectList;
}
@Override
public int getCount() {
return objectList.size();
}
@Override
public Object getItem(int position) {
return objectList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//your custom code....
return View;
}
@Override
public boolean areAllItemsEnabled() {
return super.areAllItemsEnabled();
}
//this is what you need
@Override
public boolean isEnabled(int position) {
if(objectList.get(position).yourBoolean()==false){
return false;
}
if(objectList.get(position).yourBoolean()==true && clicked==true){
return true;
}
return super.isEnabled(position);
}
以下代码会导致错误
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
//Then the isEnabled method will return false for this position and
//this item wont be clickable
objectList.get(position).setYourBooleanValue(false);
customAdapter.setclicked(true);
}
});
所以我得到的错误发生在第一个元素是一个int但为什么它期望像对象一样的字节?
答案:它与第一个元素在int中的事实无关。该列表需要是一个元组,见下文。
答案 0 :(得分:3)
解决方案是numpy需要单个元组或元组列表。 不列表或列表。
以下作品
np.array((1, 1.0), dtype={'names': names, 'formats': formats})
我觉得numpy没有在错误中表明这是预期的结果令人沮丧。