片段不更新listview

时间:2016-11-21 17:35:34

标签: android listview android-fragments android-activity

我有一个片段,其中有三个tabhost。它们中的每一个都是一个片段。让我们称他们为A,B,C。然后从Fragment 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。有人可以帮我解释原因吗?

1 个答案:

答案 0 :(得分:0)

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

所以简单的答案就是从你活动中被覆盖的onBackPressed()方法中删除super.onBackPressed(),它应该会很好。