現在我使用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;
}
}
它是'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