我在一个listView项目中有SwitchCompat,我希望每次将其更改为特定项目的ON / OFF时,将此SwitchCompat状态发送到我的远程数据库,我试图将此网络调用放入适配器但我得到了例外:NetworkOnMainThreadException
任何帮助?
我正在使用Retrofit进行网络通话
我的适配器
Adapter.java
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ExpensesHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ExpensesHolder();
...
row.setTag(holder);
} else {
holder = (ExpensesHolder) row.getTag();
}
holder.switchExpenses.setTag(position);
...
if (flag.equals("unselected")) {
holder.switchExpenses.setChecked(false);
} else {
holder.switchExpenses.setChecked(true);
}
if (TextUtils.isEmpty(expName)) {
// want to make network call here
}
return row;
}
答案 0 :(得分:1)
可能您正在调用call.execute()
,以避免使用call.enqueue(listener)