【问题标题】:Fragment not updating listview片段不更新列表视图
【发布时间】:2016-11-21 17:35:34
【问题描述】:

我有一个片段,其中有三个标签主机。它们中的每一个都是独立的片段。让我们称他们为A,B,C。然后从片段 C 我使用 startactivityonresult 开始一个活动:-

public class FavCommittee extends Fragment {
    public ArrayList<Committee> favCommitteeData = new ArrayList<>();

    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        favCommitteeData.clear();
        View rootView = inflater.inflate(R.layout.house, container, false);
        Context cont = getActivity();
        final SharedPreferences pref = getContext().getSharedPreferences("MyFav", 0);
        final SharedPreferences.Editor editor = pref.edit();
        Map<String,?> entries = pref.getAll();
        final Set<String> keys = entries.keySet();
        int count=0;
        for (String key:keys) {
            String val = pref.getString(key, null);
            String[] store = val.split(":");
            if (store[0].equals("committee"))
                count=count+1;
        }
        int max=0;
        for (String k:keys){
            String val = pref.getString(k,null);
            if(val!=null)
                max=Integer.parseInt(k);
        }
        for (int i=0;i<max+1;i++) {
            String val = pref.getString(Integer.toString(i), null);
            if (val != null) {
                String[] store = val.split(":");
                if (store[0].equals("committee")) {
                    count = count - 1;
                    new GetAllCommittees(getContext(), rootView).execute(store[1], Integer.toString(count));
                }
            }
        }        final ListView yourListView = (ListView) rootView.findViewById(R.id.house_listview);
        yourListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                Committee obj = (Committee)yourListView.getItemAtPosition(position);
                Intent intent = new Intent(FavCommittee.this.getActivity(), committee_info.class);
                intent.putExtra("Committee",obj.getCommittee_id());
                startActivityForResult(intent, 1);
            }
        });

        return rootView;
    }

在活动中,我通过以下方式关闭活动:-

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(Activity.RESULT_OK);
        finish();
    }

现在我又在片段 C 中实现了这样的 onActivityResult():

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getActivity();
        if(requestCode == 1 && resultCode == Activity.RESULT_OK) {
            Log.d("Called Again",Integer.toString(favCommitteeData.size()));
        }

事情是 onActivityResult 在活动关闭后不会被调用。谁能帮我弄清楚为什么?

【问题讨论】:

    标签: android listview android-fragments android-activity


    【解决方案1】:

    我猜你没有在FragmentC 中的onActivityResult 上设置断点。问题是这样的:我只是在自己的项目中基本做了同样的情况,调用了onActivityResult,但是resultCode是Activity.RESULT_CANCELLED。那是因为您的活动中仍然有 super.onBackPressed() 在您被覆盖的 onBackPressed() 中,并且无论之后发生什么,它都会将结果设置为 CANCELED。

    所以简单的答案是从您的活动中覆盖的onBackPressed() 方法中删除 super.onBackPressed(),它应该可以正常运行。

    【讨论】:

    • 没有活动...片段 C 在另一个片段中
    • 我实际上发现了问题,否则。我已经相应地编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多