-1
我有我的ListView的一些問題。刷新不起作用。有人能幫我嗎? 我得到了一些片段,在每一個這些是一個列表視圖。通過點擊一個listitem,你可以輸入一個值。我刷新適配器的數據集,但列表不刷新。Android:adapter notifydatasetchanged does not work
下面的代碼:
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);
}
}
}
如果您要更新適配器的數據集?您發佈的代碼沒有顯示。它只是顯示一些拋出更新數據集的刷新代碼。 – laalto
好的,我更新了refreshData方法。現在清單隻能清除。但不適合我:( – user1803153