Android:适配器notifydatasetchanged不起作用

时间:2013-09-30 11:29:33

标签: android android-listview android-fragments

我的ListView有些问题。刷新不起作用。有人能帮我吗? 我得到了一些片段,每一个都是一个列表视图。通过单击listitem,您可以输入一个值。我刷新适配器的数据集,但列表没有刷新。

这里是代码:

public class ValuationActivity extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

    private static List<Ziel> goals = new ArrayList<Ziel>();


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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        this.setTitle(ProjectsActivity.selectedProject.getKurzname());

        this.getGoals();

        mSectionsPagerAdapter.notifyDataSetChanged();

        }
}

public static class DummySectionFragment extends Fragment {

    private static TextView pointsTextView;
    private static ProgressBar progressBar;
    private static Ziel selectedGoal;
    private static List<Ziel> tempGoalList;
    private SimpleAdapter sAdapter;
    private static ArrayList<HashMap<String, String>> dataList;
    private static String[] from;
    private static int[] to;
    private static int nativeLayout;
    private static ListView listView;

    public DummySectionFragment() {
    }

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

        View rootView = inflater.inflate(R.layout.fragment_valuation_dummy, container, false);

        tempGoalList = (ValuationActivity.goals.get((getArguments().getInt(ARG_SECTION_NUMBER)) - 1)).getSubgoals();

        dataList = new ArrayList<HashMap<String, String>>(tempGoalList.size());


        this.refreshGoalList(rootView);

        progressBar = (ProgressBar) rootView.findViewById(R.id.progressBarValuation);
        pointsTextView = (TextView) rootView.findViewById(R.id.textViewPointsLeft);

        progressBar.setProgress(points);
        pointsTextView.setText(points.toString() + getString(R.string.StringPointsLeft));

        return rootView;
    }

    private void refreshGoalList(View rootView) {

        tempGoalList = (ValuationActivity.goals.get((getArguments().getInt(ARG_SECTION_NUMBER)) - 1)).getSubgoals();

        listView = (ListView) rootView.findViewById(id.listViewSubgoals);

        refreshData();

        from = new String[] { "name", "gewichtung" };
        to = new int[] { android.R.id.text1, android.R.id.text2 };
        nativeLayout = android.R.layout.simple_list_item_2;
        sAdapter = new SimpleAdapter(getActivity(), dataList, nativeLayout, from, to);

        listView.setAdapter(sAdapter);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                tempGoalList = (ValuationActivity.goals.get((getArguments().getInt(ARG_SECTION_NUMBER)) - 1)).getSubgoals();

                AlertDialog.Builder valuationBox = new AlertDialog.Builder(getActivity());
                selectedGoal = tempGoalList.get(arg2);

                valuationBox.setTitle(selectedGoal.getName() + ": " + selectedGoal.getBeschreibung());
                valuationBox.setMessage("Bitte geben Sie Ihre Bewertung ein. Sie haben noch " + points + " Punkte zur Verfügung.");

                // Set an EditText view to get user input
                final EditText input = new EditText(getActivity());
                input.setInputType(InputType.TYPE_CLASS_NUMBER);

                valuationBox.setView(input);

                valuationBox.setPositiveButton("Bewerten", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        try {
                            Integer value = Integer.parseInt(input.getText().toString());
                            System.out.println("Eingegangene Bewertung: " + value);

                            points = points + selectedGoal.getGewichtung() - value;

                            selectedGoal.setGewichtung(value);

                            progressBar.setProgress(points);
                            pointsTextView.setText(points.toString() + getString(R.string.StringPointsLeft));


                        } catch (NumberFormatException nfe) {
                            System.out.println("Could not parse " + nfe);
                        }
                        // TODO - refresh GoalList
                        DummySectionFragment.refreshData();

                        sAdapter.notifyDataSetChanged();
                    }

                });

                valuationBox.create().show();
            }
        });
    }

    private static void refreshData() {

        dataList.clear();
        for (Ziel tempZiel : tempGoalList) {

            HashMap<String, String> item = new HashMap<String, String>();
            item.put("name", tempZiel.getBeschreibung());
            item.put("gewichtung", tempZiel.getGewichtung().toString() + " Punkte");

            System.out.println(tempZiel.getBeschreibung() + ": " + tempZiel.getGewichtung());

            dataList.add(item);
        }



    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是您正在使用专为静态内容设计的SimpleAdapter。 所以你应该实现自己的ListAdapter。

见:

http://developer.android.com/reference/android/widget/SimpleAdapter.html http://developer.android.com/reference/android/widget/ListAdapter.html

How to update SimpleAdapter in Android