将值从extra传递到cursor

将值从extra传递到cursor

本文介绍了将值从extra传递到cursor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决我的应用程序中的最后几个错误。与我目前的设置我有用户通过一系列listview(类别 - >源 - >标题)。基于listview的位置,我传递一个int,我的光标用来过滤结果,并给出属于以前点击的项目的项目。在通过不同的活动导航几次后,它崩溃,因为应用程序丢失了所述int的值(通过活动返回和前进)。我试图设置一个getextra(),所以它不会失去这个值。我有它的设置,以获取这些int,但有麻烦链接到我的游标,因为它想要以静态方式访问这些int,但我不会得到他们在一个静态的方式。

I'm trying to iron out the last few bugs in my application. With my current setup I have the user go through a series of listview (Category -> source -> title). Based on the position from the listview I pass a int that is used by my cursor to filter the results and give the items that belong to the previously clicked item. After navigating a few times through the different activities it crashes since the application loses the value of said int (going back and forward through the activities). I'm trying to setup a getextra() so it will not lose this value. I've got it setup to put and get these ints but having trouble linking them to my cursor since it wants to access these int in a static way but I'm not getting them in a static way.

public class title extends ListActivity {

    Bundle extras = getIntent().getExtras();
    int categoryClick = extras.getInt("cateClick");
    int sourceClick = extras.getInt("sourceclick");

...

@Override
protected void onListItemClick(ListView list, View v, int position, long id)
{
    super.onListItemClick(list, v, position, id);
    titleClick = position;
    final Intent intent = new Intent(this, inputpage.class);
    intent.putExtra("cateclick", categoryClick);
    intent.putExtra("sourceclick", sourceClick);
    intent.putExtra("titleclick", titleClick);
    startActivity(intent);
    }

我的光标与我使用的不一样看起来像这样

My cursor unchanged from what I was using looks like this

// retrieves all the descriptions for the edittext fields
      public  Cursor getUserWord()
        {
            return myDataBase.query(USER_WORD_TABLE, new String[] {
                    KEY_ID,
                    KEY_CATEGORY,
                    KEY_SOURCE, KEY_TITLE, KEY_USERWORD, KEY_QUICK
                    },
                    KEY_CATEGORY+ "=" + categories.categoryClick + " AND " + KEY_SOURCE+ "="
                    +source.sourceClick + " AND " + KEY_TITLE+ "=" + title.titleClick,
                    null, null, null, KEY_ID);

        }

我的数据库有多个表,是以下图片。

My database has multiple tables, the one for the above cursor is the image below.

这个设置可能不是得到我想要的结果的最好的方法,但是对于android,sqlite和java是新的,这是我能够为我所需要的工作。

This setup may not be the best method of getting the result I want but being new to android, sqlite and java it was what I was able to get to work for what I needed.

我在尝试切换到getextra之前遇到的错误是

The error I was getting before trying to switch to getextra was

03-10 16:06:14.653: E/AndroidRuntime(939): Uncaught handler: thread main exiting due to uncaught exception
03-10 16:06:14.683: E/AndroidRuntime(939): java.lang.NullPointerException
03-10 16:06:14.683: E/AndroidRuntime(939):  at wanted.pro.madlibs.source.onListItemClick(source.java:55)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.app.ListActivity$2.onItemClick(ListActivity.java:312)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.widget.ListView.performItemClick(ListView.java:3285)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.os.Handler.handleCallback(Handler.java:587)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.os.Looper.loop(Looper.java:123)
03-10 16:06:14.683: E/AndroidRuntime(939):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-10 16:06:14.683: E/AndroidRuntime(939):  at java.lang.reflect.Method.invokeNative(Native Method)
03-10 16:06:14.683: E/AndroidRuntime(939):  at java.lang.reflect.Method.invoke(Method.java:521)
03-10 16:06:14.683: E/AndroidRuntime(939):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-10 16:06:14.683: E/AndroidRuntime(939):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-10 16:06:14.683: E/AndroidRuntime(939):  at dalvik.system.NativeStart.main(Native Method)

这是一个屏幕截图分解了发生了什么。

将int更改为static

更改为static之后

Here is a screen shot break down of what's going on.After changing int to staticafter changing extra to static

推荐答案

你不应该对你的Bundle进行静态引用。将它们作为实例变量,并在每次调用onCreate或onResume方法时设置它们。

Based on the screenshots, you shouldn't be making static references to your Bundles. Make them instance variables, and set them every time your onCreate or onResume method gets called.

这篇关于将值从extra传递到cursor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:57