SimpleCursorAdapter没有返回位置

时间:2013-08-13 08:17:13

标签: android simplecursoradapter

我在stackoverflow中搜索了类似的问题。这些解决方案似乎都不起作用。

这是我的CustomAdapter类。

public class ContactAdapter extends SimpleCursorAdapter {

Context cont;

public ContactAdapter(Context context, int layout, Cursor c, String[] from,
        int[] to) {
    super(context, layout, c, from, to, 0);
    cont = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

    Button button = (Button)view.findViewById(R.id.delete_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            Toast.makeText(cont, position,
                    Toast.LENGTH_SHORT).show();
        }
    }); 

    return view;
}
}

根据文档,我应该在吐司中获得当前项目的位置。但它引发了以下例外情况。

08-13 13:46:01.904: E/AndroidRuntime(5980): android.content.res.Resources$NotFoundException: String resource ID #0x0
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.content.res.Resources.getText(Resources.java:265)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.widget.Toast.makeText(Toast.java:265)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at com.example.databaseproject.ContactAdapter$1.onClick(ContactAdapter.java:34)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.view.View.performClick(View.java:4091)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.view.View$PerformClick.run(View.java:17072)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.os.Handler.handleCallback(Handler.java:615)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.os.Looper.loop(Looper.java:153)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at android.app.ActivityThread.main(ActivityThread.java:5034)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at java.lang.reflect.Method.invoke(Method.java:511)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-13 13:46:01.904: E/AndroidRuntime(5980):     at dalvik.system.NativeStart.main(Native Method)

请帮忙。

2 个答案:

答案 0 :(得分:4)

问题是position是一个int,而Toast.makeText方法的第二个参数(如果它是int)必须是资源ID。因此,此处没有ID为position的资源,您将获得Resources$NotFoundException

如何解决?

只需按position + ""

将其转换为字符串即可

使用此代码

   button.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            Toast.makeText(cont, position+"",
                    Toast.LENGTH_SHORT).show();
        }
    }); 

答案 1 :(得分:1)

我认为您已在int中传递Toast值,导致异常

按照以下方式更改Toast ..

Toast.makeText(cont, String.valueOf(position),Toast.LENGTH_SHORT).show();