代码之家  ›  专栏  ›  技术社区  ›  TheUnreal

从其他活动更新ListView

  •  0
  • TheUnreal  · 技术社区  · 10 年前

    活动1: 包含ListView。

    活性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 回复  |  直到 10 年前
        1
  •  1
  •   SuperFrog    10 年前

    如果适配器的备份数据发生更改,则应通知适配器。 在您的场景中,如果数据发生更改,您可能会考虑添加一个签入onResume方法,如果数据确实发生了更改,请通知适配器:

    @Override
    protected void onResume() {
        super.onResume();
    
        //if the data has changed
        mJSONAdapter.notifyDataSetChanged(); 
    }
    
        2
  •  0
  •   diyoda_    10 年前

    这只是一个空Pinter异常问题。但基本步骤如下

    创建适配器并保留它。还应参考您传递到适配器中的项目列表,当您从活动2作为Udi I时,您可以清除项目列表并将新项目添加到其中(不要将项目添加为新列表,如果这样做,则必须重新实例化适配器并重置适配器)。然后呼叫 notifyDataSetChanged() 在适配器上。

    编辑1:

    我建议您更多地研究“活动”生命周期以及“活动”和“片段”之间的通信。我假设您在活动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不为空。

        3
  •  0
  •   TheUnreal    10 年前

    我解决了这个问题:

            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);
                    }
                });
    
    推荐文章