-1

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

这里的代码:

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);
        }



    }
}
4

1 回答 1

0

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

看:

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

如何在 Android 中更新 SimpleAdapter

于 2013-09-30T11:55:15.153 回答