从另一个活动

时间:2015-08-27 16:00:06

标签: android android-listview

活动1:包含ListView。

Acitivity 2:包含更改ListView使用的SharedPreferences变量的SeekBar。更改SeekBar并返回活动1后,需要更新和刷新ListView。

在活动2中更新SeekBar后,我使用以下代码返回活动1:

        toolbar.setNavigationIcon(R.drawable.ic_arrow_forward_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//I need to update the list here, There may be a new data!
                onBackPressed();

            }
        });

我的活动1仍然是未更新的ListView。

在更改Seekbar后,我找不到在活动1中刷新列表的好方法,因为适配器仅在活动1中可用。 我试图将其作为意图传递,但我注意到它不可能将适配器作为额外内容传递。

任何帮助?

代码我用来更新活动1中的ListView:

mainListView = (ListView) findViewById(R.id.main_listview);
                    // 5. Set this activity to react to list items being pressed
                    //mainListView.setOnItemClickListener(MainActivity.this);
                    // 10. Create a JSONAdapter for the ListView
                    mJSONAdapter = new JSONAdapter(MainActivity.this, getLayoutInflater());
                    // Set the ListView to use the ArrayAdapter
                    mainListView.setAdapter(mJSONAdapter);

尝试Udi的解决方案后出现Logcat错误:

08-27 12:58:29.400    1595-1595/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: il.co.test.test, PID: 1595
    java.lang.RuntimeException: Unable to resume activity {il.co.test.test/il.co.test.test.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at il.co.test.test.MainActivity.onResume(MainActivity.java:334)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
            at android.app.Activity.performResume(Activity.java:5310)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

编辑2:完整代码我如何更新我的列表 - 在GPS跟踪之后。放在MainActivity.java中。在TabFragment1.java中调用(下面的代码)

public void getUpdates()
{
    // Access the device's key-value storage
    mSharedPreferences = getSharedPreferences(PREFS, MODE_PRIVATE);
    searchDistance = mSharedPreferences.getInt(PREF_DISTANCE, 0);
    if (searchDistance == 0)
        searchDistance = DEFAULT_SEARCH_DISTANCE;

        gpsDialog = new ProgressDialog(this);
        gpsDialog.setMessage("Checking your location..");
        gpsDialog.setCancelable(false);

        gpsDialog.show();

        MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
            @Override
            public void gotLocation(Location location) {
                gpsDialog.dismiss();
                //If we find a location, We will go to the list layout.
                //setContentView(R.layout.fb_list);
                // 4. Access the ListView
                mainListView = (ListView) findViewById(R.id.main_listview);
                // 5. Set this activity to react to list items being pressed
                //mainListView.setOnItemClickListener(MainActivity.this);
                // 10. Create a JSONAdapter for the ListView
                mJSONAdapter = new JSONAdapter(MainActivity.this, getLayoutInflater());
                // Set the ListView to use the ArrayAdapter
                mainListView.setAdapter(mJSONAdapter);
                double lat = location.getLatitude();
                double lon = location.getLongitude();
                query(lat, lon, searchDistance);
            }
        };
        MyLocation myLocation = new MyLocation(this);
        myLocation.getLocation(this, locationResult);
}

从我的片段中调用此方法:

    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.fb_list, container, false);
    MainActivity king = (MainActivity)getActivity();
    king.getUpdates();
    return v;
}

当我添加此内容时:

@Override
protected void onResume() {
    super.onResume();

    //if the data has changed
    mJSONAdapter.notifyDataSetChanged();
}

应用程序崩溃,因为它无法找到json适配器(因为只有在gps位置后它才会被初始化)

3 个答案:

答案 0 :(得分:1)

如果更改了适配器的后备数据,则应通知适配器。 在您的场景中,如果数据已更改,您可以考虑添加一个onResume方法检查,如果更改了,则通知适配器:

@Override
protected void onResume() {
    super.onResume();

    //if the data has changed
    mJSONAdapter.notifyDataSetChanged(); 
}

答案 1 :(得分:0)

这只是Null Pinter异常的问题。但基本步骤如下

创建适配器并保留它。保持对您传递到适配器的项目列表的引用,当您来自活动2作为Udi时,您可以清除项目列表并向其添加新项目(不要将项目添加为新列表,如果这样做,则必须再次重新实例化适配器并重置适配器)。然后调用适配器上的notifyDataSetChanged()

EDIT1:

我建议你更多地调查Activity生命周期以及Activity和Fragment之间的沟通。我假设你在Activity 1中切换片段(TabFragment1)。

我不推荐这个,但你可以试试这个,

@Override
protected void onRestart() {
    super.onRestart();
    getUpdates();
    mainListView.setAdapter(mJSONAdapter);
}

但你必须确保,

// 10. Create a JSONAdapter for the ListView
                mJSONAdapter = new JSONAdapter(MainActivity.this, getLayoutInflater());

这个mJsonAdapter不为空。

答案 2 :(得分:0)

我通过替换它来解决它:

        toolbar.setNavigationIcon(R.drawable.ic_arrow_forward_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//I need to update the list here, There may be a new data!
                onBackPressed();

            }
        });

用这个:

toolbar.setNavigationIcon(R.drawable.ic_arrow_forward_white_24dp);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent refresh = new Intent(Settings.this, MainActivity.class);
                    Settings.this.startActivity(refresh);
                }
            });