无法更新 ListFragment 中的值

unable to update values in ListFragment

我正在尝试保存 ListFragment 中的值,因为它们在 HashMap 中被单击,并试图通过单击按钮访问它们,但 hashmap 始终为空。 在我的切换值函数中,我可以看到哈希映射正在更新,但是当我在按钮单击侦听器上访问它时,它是空的。 这些片段有什么我遗漏的吗?

public class SListFragment extends ListFragment {
private Button button;
private HashMap<Integer, String> hmap = new HashMap<Integer, String>();
View rootView;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    adapter = new listAdapter(
            getActivity().getApplication(), list);
    setListAdapter(adapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {      
    toggleValues(position);
}

@Override
public int getSelectedItemPosition() {
    return super.getSelectedItemPosition();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    cButton = (Button) getActivity().findViewById(R.id.btn_contribute);
    cButtonOnClickListener();
    return super.onCreateView(inflater, container, savedInstanceState);
}

private void cButtonOnClickListener(){


    cButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cButton.setText("ok");
            Intent cIntent = new Intent(getActivity().getApplication(), CActivity.class);                
            cIntent.putExtra("position", hmap);
            //here hmap is empty; I don't understand  why?

            startActivity(cIntent);
        }
    });
}

private void toggleValues(int position){

    if(hmap.containsKey(position)){
        hmap.remove(position);
    }
    else {
        hmap.put(position,"position");
    }
}

}

我用你的 post 写了一个演示。错误未出现:

public class SListFragment extends ListFragment {
    private Button cButton;
    private HashMap<Integer, String> hmap = new HashMap<Integer, String>();
    private List<String> list = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    View rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("six");
        list.add("seven");
        adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1,
                list);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        toggleValues(position);
    }

    @Override
    public int getSelectedItemPosition() {
        return super.getSelectedItemPosition();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.activity_text_demo, container, false);
        cButton = (Button) rootView.findViewById(R.id.btn_contribute);
        cButtonOnClickListener();
        return rootView;
    }

    private void cButtonOnClickListener() {

        cButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cButton.setText("ok");
                Intent cIntent = new Intent(getActivity().getApplication(), MainActivity.class);
                cIntent.putExtra("position", hmap);
                Set<Entry<Integer, String>> sets = hmap.entrySet();
                Iterator<Entry<Integer, String>> it = sets.iterator();
                while (it.hasNext()) {
                    Entry<Integer, String> entry = it.next();
                    Log.d("demo", "hmap key: " + entry.getKey() + " --> value: " + entry.getValue());
                }

                // here hmap is empty; I don't understand why?

                // startActivity(cIntent);
            }
        });
    }

    private void toggleValues(int position) {

        if (hmap.containsKey(position)) {
            hmap.remove(position);
        }
        else {
            hmap.put(position, "position");
        }
    }
}

这是片段使用的布局文件activity_text_demo

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

   <Button 
        android:id="@+id/btn_contribute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Button" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我重写了测试代码。我在 MainActivity 的 xml 文件中放置了一个按钮,并使用列表片段中包含的默认列表视图:

MainActivity.java:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (findViewById(R.id.content_container) != null && savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.content_container, new SListFragment()).commit();

        }
    }

}

这是 activity 的 xml 文件 activity_main.xml:

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

   <Button 
        android:id="@+id/btn_contribute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Button" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

这是列表片段SListFragment.java。实际上,@peaceofmind 的代码 post 很熟悉:

public class SListFragment extends android.support.v4.app.ListFragment {
    private Button cButton;
    private HashMap<Integer, String> hmap = new HashMap<Integer, String>();
    private List<String> list = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    View rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("six");
        list.add("seven");
        adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1,
                list);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        toggleValues(position);
    }

    @Override
    public int getSelectedItemPosition() {
        return super.getSelectedItemPosition();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        cButton = (Button) getActivity().findViewById(R.id.btn_contribute);
        cButtonOnClickListener();

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void cButtonOnClickListener() {

        cButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cButton.setText("ok");
                Intent cIntent = new Intent(getActivity().getApplication(), MainActivity.class);
                cIntent.putExtra("position", hmap);
                Set<Entry<Integer, String>> sets = hmap.entrySet();
                Iterator<Entry<Integer, String>> it = sets.iterator();
                while (it.hasNext()) {
                    Entry<Integer, String> entry = it.next();
                    Log.d("demo", "hmap key: " + entry.getKey() + " --> value: " + entry.getValue());
                }

                // here hmap is empty; I don't understand why?

                // startActivity(cIntent);
            }
        });
    }

    private void toggleValues(int position) {

        if (hmap.containsKey(position)) {
            hmap.remove(position);
        }
        else {
            hmap.put(position, "position");
        }
    }
}