Android:可以将参数从 CursorAdapter 传递到服务

标签 android android-service android-cursoradapter

我只想将从游标获取的变量传递给服务。但总是得到一个 NullPointerException

这是我的适配器,我用它来将数据显示到 listView 中。

public class DetailCategoryCursorAdapter extends CursorAdapter{

private Services services;

public DetailCategoryCursorAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_item_detail, parent, false);
}

@Override
public void bindView(View view, final Context context, Cursor cursor) {

TextView titleTextView = (TextView) view.findViewById(R.id.title);
        ImageView imageView = (ImageView) view.findViewById(R.id.dplay);

int titleColumnIndex = cursor.getColumnIndex(Contract.Entry.COLUMN_TITLE);

int urlColumnIndex = cursor.getColumnIndex(Contract.Entry.COLUMN_URL);

    String title = cursor.getString(titleColumnIndex);
    String url = cursor.getString(urlColumnIndex);

    nameTextView.setText(title);

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent sIntent = new Intent(context, Services.class);
                sIntent.putExtra("url", url);
                services.startService(sIntent); // in this part always show NullPointerEception
                Log.v("url", url);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}
}

或者有更好的方法吗?需要建议。谢谢

最佳答案

在您的 onBind 中使用它不会抛出异常。 (因为你的services是空的)

Intent sIntent = new Intent(context, Services.class);
            sIntent.putExtra("url", url);
            context.startService(sIntent);

关于Android:可以将参数从 CursorAdapter 传递到服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49025936/

相关文章:

Android - CursorAdapter 不工作

Android - 自定义 AutoCompleteTextView CursorAdaptor - 建议行为

android - Maps v2 标记动画 - 淡入淡出

android - Android Studio 中测试支持库的来源

android - 在 Android 上首次加载时不显示相机预览

android - Android Service中如何使用MutableSharedFlow?

android - CursorAdapter 破坏了 CHOICE_MODE_MULTIPLE 选项

java - 从 getInstalled 应用程序中过滤 SystemApp

android - 为 ACTION_BOOT_COMPLETED 动态注册 BroadcastReceiver 不起作用?

安卓.content.res.Resources$NotFoundException : String resource id #0x64