Android:适配器notifydatasetchanged不工作
我的ListView有一些问题。刷新不起作用。有人能帮我吗? 我得到了一些片段,每个片段中都有一个列表视图。通过单击列表项,您可以输入一个值。我刷新了适配器的数据集,但列表没有刷新 代码如下:Android:适配器notifydatasetchanged不工作,android,android-listview,android-fragments,Android,Android Listview,Android Fragments,我的ListView有一些问题。刷新不起作用。有人能帮我吗? 我得到了一些片段,每个片段中都有一个列表视图。通过单击列表项,您可以输入一个值。我刷新了适配器的数据集,但列表没有刷新 代码如下: public class ValuationActivity extends FragmentActivity { SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; private static
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);
}
}
}
公共类求值活动扩展了碎片活动{
分段SPAGERADAPTER mSectionsPagerAdapter;
ViewPager mViewPager;
私有静态列表目标=新建ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_valuation);
mSectionsPagerAdapter=newsectionspageradapter(getSupportFragmentManager());
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
this.setTitle(ProjectsActivity.selectedProject.getKurzname());
这个.getGoals();
mSectionsPagerAdapter.notifyDataSetChanged();
}
}
公共静态类DummySectionFragment扩展了片段{
私有静态TextView点TextView;
私有静态ProgressBar ProgressBar;
私有静态Ziel selectedGoal;
私有静态列表tempGoalList;
私人单纯形;
私有静态ArrayList数据列表;
来自的私有静态字符串[];
私有静态int[]到;
私有静态int本地布局;
私有静态ListView ListView;
公共数据段(){
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.fragment\u valuation\u dummy,容器,false);
tempGoalList=(evaluationActivity.goals.get((getArguments().getInt(ARG_SECTION_NUMBER))-1)).getSubgoals();
dataList=newarraylist(tempGoalList.size());
这个.refreshGoalList(rootView);
progressBar=(progressBar)rootView.findViewById(R.id.progressBarValuation);
pointsTextView=(TextView)rootView.findViewById(R.id.textViewPointsLeft);
进度条。设置进度(点);
pointsTextView.setText(points.toString()+getString(R.string.StringPointsLeft));
返回rootView;
}
私有void刷新goallist(视图rootView){
tempGoalList=(evaluationActivity.goals.get((getArguments().getInt(ARG_SECTION_NUMBER))-1)).getSubgoals();
listView=(listView)rootView.findViewById(id.listViewSubgoals);
刷新数据();
from=新字符串[]{“name”,“gewichtung”};
to=newint[]{android.R.id.text1,android.R.id.text2};
nativeLayout=android.R.layout.simple_list_item_2;
sAdapter=new simpledapter(getActivity(),dataList,nativeLayout,from,to);
setAdapter(sAdapter);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
tempGoalList=(evaluationActivity.goals.get((getArguments().getInt(ARG_SECTION_NUMBER))-1)).getSubgoals();
AlertDialog.Builder valuationBox=新建AlertDialog.Builder(getActivity());
selectedGoal=tempGoalList.get(arg2);
evaluationBox.setTitle(selectedGoal.getName()+”:“+selectedGoal.getBeschreibung());
估值框。设置消息(“你的估值是多少?”+点数+“你的估值是多少?”);
//设置EditText视图以获取用户输入
最终编辑文本输入=新编辑文本(getActivity());
input.setInputType(InputType.TYPE\类别\编号);
valuationBox.setView(输入);
valuationBox.setPositiveButton(“Bewerten”,新的DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
试一试{
整数值=Integer.parseInt(input.getText().toString());
System.out.println(“Eingegangene Bewertung:+值);
点=点+selectedGoal.getGewichtung()-值;
选择gegoal.setGewichtung(值);
进度条。设置进度(点);
pointsTextView.setText(points.toString()+getString(R.string.StringPointsLeft));
}捕获(NumberFormatException nfe){
System.out.println(“无法解析”+nfe);
}
//待办事项-刷新守门员
DummySectionFragment.refreshData();
sAdapter.notifyDataSetChanged();
}
});
evaluationBox.create().show();
}
});
}
私有静态无效刷新数据(){
dataList.clear();
for(Ziel tempZiel:tempGoalList){
HashMap项=新建HashMap();
item.put(“name”,tempZiel.getBeschreibung());
item.put(“gewichtung”,tempZiel.getGewichtung().toString()+“Punkte”);
System.out.println(tempZiel.getBeschreibung()+”:“+tempZiel.getGewichtung());
数据列表。添加(项);
}
}
}
您的问题是使用的是专为静态内容设计的SimpleAdapter。
因此,您可能应该实现自己的ListAdapter
见:
在哪里更新适配器数据集?你发布的代码没有显示这一点。它只是显示了一些丢弃更新数据集的刷新代码。好的,我更新了refreshData方法。现在这个名单才刚刚被清除。但对我来说不起作用:(