ListView中的SwitchCompat进行网络调用
SwitchCompat in ListView to make network call
我在 listView 项中有 SwitchCompat,我希望每次更改它时将此 SwitchCompat 状态发送到我的远程数据库 ON/OFF 对于特定项,我尝试将此网络调用放入适配器中但我得到了异常: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;
}
可能您正在调用 call.execute()
,为避免 NetworkOnMainThreadException 使用 call.enqueue(listener)
我在 listView 项中有 SwitchCompat,我希望每次更改它时将此 SwitchCompat 状态发送到我的远程数据库 ON/OFF 对于特定项,我尝试将此网络调用放入适配器中但我得到了异常: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;
}
可能您正在调用 call.execute()
,为避免 NetworkOnMainThreadException 使用 call.enqueue(listener)