活动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定位后才初始化)