ListView中的SwitchCompat进行网络调用

时间:2018-05-06 14:16:48

标签: android listview retrofit retrofit2 switchcompat

我在一个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;

}

1 个答案:

答案 0 :(得分:1)

可能您正在调用call.execute(),以避免使用call.enqueue(listener)

来避免NetworkOnMainThreadException