如何获取notifyDatasetChanged()以使用ListAdapter?
现在我使用setAdapter来更新我的ListView,但我认为正确的方法是使用notifiyDatasetChanged(),我无法让它在我的主类(它在适配器中)中工作。以下是错误:如何获取notifyDatasetChanged()以使用ListAdapter?
的方法notifyDatasetChanged()是未定义的类型ListAdapter
我猜测有这样做的更好的方法 - 任何人都可以点我在正确的方向?
这里是我的代码的相关部分:
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
listViewScore.setAdapter(new ScoreListAdapter(ctx,
R.layout.score_row_item, listScore));
listViewScore.getAdapter().notifyDatasetChanged(); //this is where I get the error
}
}
这里的适配器:
public class ScoreListAdapter extends ArrayAdapter<Score> {
private int resource;
private LayoutInflater inflater;
public ScoreListAdapter(Context ctx, int resourceId, List<Score> objects) {
super(ctx, resourceId, objects);
resource = resourceId;
inflater = LayoutInflater.from(ctx);
//context = ctx;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout) inflater.inflate(resource, null);
Score score = getItem(position);
TextView txtName = (TextView) convertView.findViewById(R.id.name);
txtName.setText(score.getName());
TextView txtScoreChange = (TextView) convertView
.findViewById(R.id.scoreChange);
int scoreChange = Integer.parseInt(score.getScoreChange());
if (scoreChange > 0)
txtScoreChange.setText("+" + scoreChange);
else if (scoreChange < 0)
txtScoreChange.setText("" + scoreChange);
else
txtScoreChange.setText("");
TextView txtScoreTotal = (TextView) convertView
.findViewById(R.id.scoreTotal);
txtScoreTotal.setText(score.getScoreTotal());
final LinearLayout currentRow = (LinearLayout) convertView
.findViewById(R.id.scoreRowLayout);
notifyDataSetChanged();
return convertView;
}
}
创建自定义的适配器的情况下,这样你就可以在任何地方使用它,你喜欢......
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
private ScoreListAdapter adapter;
static List<Score> listScore = new ArrayList<Score>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
listViewScore.setAdapter(adapter);
adapter.notifyDatasetChanged();
}
}
顺便说一句,如果你的listScore阵列已经被加载,那么你就需要使用
adapter.notifyDatasetChanged();
谢谢,这是一个非常简单的解释,它完美的作品(除了我必须大写设置在notifyDataSetChanged();作为别人指出) – GrilledCheese 2013-02-13 18:54:35
啊,是的,我只是忽略它:)但正如我所说,这是没有必要如果您的列表已经加载,请在您的情况下使用它。 – yahya 2013-02-13 19:55:52
不要调用notifyDataSetChanged();方法而创造。
只把它当你的listViewScore的内容改变..并使用它在那个时间
更换
listView.getAdapter().notifyDatasetChanged();
与
((ScoreListAdapter)listView.getAdapter()).notifyDataSetChanged();
,看到了魔术......
谢谢。
它是'notifyDataSetChanged()' - 注意大写字母S.你有一个小写's'你显示有错误的行。 – Squonk 2013-02-13 18:02:00
首先,您需要知道notifyDatasetChanged的用法...通知附加的观察者已经更改了底层数据,并且任何反映该数据集的View都应该自行刷新。在实现...之前了解这一点... notifyDatasetChanged()将用于适配器数据中的数据更新或者简单地更改..即与适配器关联的集合已更改。不需要在创建时调用它 – Pragnani 2013-02-13 18:02:06
谢谢,我剪掉了很多代码,所以我没有在创建时调用它,它只是在这里查看。 – GrilledCheese 2013-02-13 18:54:56