如何在不重新启动应用程序的情况下更新片段中的列表视图?

时间:2019-03-28 14:37:09

标签: android android-fragments

我有一个带有ListView的片段,该片段列出了在后台运行的非系统应用程序。我希望在启动和关闭新应用程序时进行更新。我不确定如何应用其他答案,所以我想应该问。

主要活动

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

private final Fragment mTimerFragment = new TimerFragment();
private final Fragment mListFragment = new ListFragment();
private final Fragment mReminderFragment = new RemindFragment();

private Fragment mActiveFragment = mTimerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportFragmentManager().beginTransaction()
        .add(R.id.fragContainer, mReminderFragment, "TimerFragment")
        .hide(mReminderFragment)
        .commit();

        getSupportFragmentManager().beginTransaction()
        .add(R.id.fragContainer, mListFragment, "ListFragment")
        .hide(mListFragment)
        .commit();

        getSupportFragmentManager().beginTransaction()
        .add(R.id.fragContainer, mTimerFragment, "ReminderFragment")
        .commit();

        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(this);
        }

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {

        case R.id.navigation_home:
        getSupportFragmentManager().beginTransaction()
        .hide(mActiveFragment)
        .show(mTimerFragment)
        .commit();
        mActiveFragment = mTimerFragment;
        return true;

        case R.id.navigation_dashboard:
        getSupportFragmentManager().beginTransaction()
        .hide(mActiveFragment)
        .show(mListFragment)
        .commit();
        mActiveFragment = mListFragment;
        return true;

        case R.id.navigation_notifications:
        getSupportFragmentManager().beginTransaction()
        .hide(mActiveFragment)
        .show(mReminderFragment)
        .commit();
        mActiveFragment = mReminderFragment;
        return true;
        }

        return false;
        }
        }

我正在使用带有三个片段的底部导航。问题片段是这个:

片段

public class ListFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_list,null);
        ListView list1 = (ListView)view.findViewById(R.id.listview1);
        ActivityManager am = (ActivityManager)getActivity().getSystemService(Context.ACTIVITY_SERVICE);
        PackageManager packageManager = getActivity().getPackageManager();

        List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
        final String pnArr[] = new String[runningAppProcessInfo.size()];
        List<String> filteredApps = new ArrayList<>();
        List<String> filteredRunningApps = new ArrayList<>();
        List<String> appNames = new ArrayList<>();

        //user-apps loop
        List<PackageInfo> apps = getActivity().getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < apps.size(); i++){
            if ((apps.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1){
                filteredApps.add(apps.get(i).packageName);
            }
        }

        //running apps + comparison loop
        for (int i = 0; i < runningAppProcessInfo.size(); i++) {
            pnArr[i] = runningAppProcessInfo.get(i).processName;
            if (filteredApps.contains(runningAppProcessInfo.get(i).processName)) {
                filteredRunningApps.add(runningAppProcessInfo.get(i).processName);
            }
        }

        //package name -> app name conversion
        for (int i = 0; i < filteredRunningApps.size();i++) {
            ApplicationInfo ai = null;
            try{
                ai = packageManager.getApplicationInfo(filteredRunningApps.get(i),0);

            }catch (PackageManager.NameNotFoundException e){}
            String title = (String)((ai != null) ? packageManager.getApplicationLabel(ai) : "???");
            appNames.add(title);
        }

        Toast.makeText(getActivity(), "Filtered Apps : " + filteredApps.size() + " | Running Apps : " + runningAppProcessInfo.size() + " | Filtered Running Apps : " + filteredRunningApps.size(), Toast.LENGTH_LONG).show();

         ArrayList<String> pnList = new ArrayList<String>();
         pnList.addAll(Arrays.asList(pnArr));
         ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,appNames);
         list1.setAdapter(aa);

        return view;
    }
}

它现在的工作方式是在应用程序启动时获取列表,但一直保持这种状态,直到应用程序重新启动并用新值填充列表为止。知道在关闭/打开后台应用程序时如何使其更新吗?

1 个答案:

答案 0 :(得分:0)

更新数据时,可以在适配器上调用notifyDataSetChanged()

因此,在您的情况下,将在此行之后

  appNames.add(title);

您添加了

  aa.notifyDataSetChanged()