2013-02-13 70 views
5

現在我使用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; 
    } 
} 
+0

它是'n​​otifyDataSetChanged()' - 注意大寫字母S.你有一個小寫's'你顯示有錯誤的行。 – Squonk 2013-02-13 18:02:00

+0

首先,您需要知道notifyDatasetChanged的用法...通知附加的觀察者已經更改了底層數據,並且任何反映該數據集的View都應該自行刷新。在實現...之前瞭解這一點... notifyDatasetChanged()將用於適配器數據中的數據更新或者簡單地更改..即與適配器關聯的集合已更改。不需要在創建時調用它 – Pragnani 2013-02-13 18:02:06

+0

謝謝,我剪掉了很多代碼,所以我沒有在創建時調用它,它只是在這裏查看。 – GrilledCheese 2013-02-13 18:54:56

回答

3

創建自定義的適配器的情況下,這樣你就可以在任何地方使用它,你喜歡......

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(); 
+0

謝謝,這是一個非常簡單的解釋,它完美的作品(除了我必須大寫設置在notifyDataSetChanged();作爲別人指出) – GrilledCheese 2013-02-13 18:54:35

+0

啊,是的,我只是忽略它:)但正如我所說,這是沒有必要如果您的列表已經加載,請在您的情況下使用它。 – yahya 2013-02-13 19:55:52

1

不要調用notifyDataSetChanged();方法而創造。

只把它當你的listViewScore的內容改變..並使用它在那個時間

更換

listView.getAdapter().notifyDatasetChanged(); 

((ScoreListAdapter)listView.getAdapter()).notifyDataSetChanged(); 

,看到了魔術......

謝謝。

相關問題