簡體   English   中英

從另一個活動更新ListView

[英]Updating ListView from another Activity

活動1:包含ListView。

Acitivity 2:包含SeekBar,它可以更改ListView使用的SharedPreferences變量。 更改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位置之后才初始化)

如果adapater的后備數據已更改,則應通知適配器。 在您的方案中,您可能會考慮在onResume方法中添加一個檢查,如果數據已更改,並且如果確實更改了,則通知適配器:

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

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

這只是一個空品特例外問題。 但是基本步驟如下

創建適配器並保留它。 還保留對傳遞給適配器的項目列表的引用,當您從活動2來時,作為Udi我可以清除項目列表並向其中添加新項目(不要將項目添加為新列表) ,如果這樣做,則必須重新實例化適配器並重置適配器)。 然后在適配器上調用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不為null。

我通過替換它解決了這個問題:

        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);
                }
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM