2013-02-13 3 views
5

今、私はListViewを更新するためにsetAdapterを使用していますが、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

これは 'notifyDataSetChanged()'です。大文字であることがわかります。表示されている行に小文字の 's'があります。エラーがあります。 – Squonk

+0

まず、notifyDatasetChangedの使用方法を知っておく必要があります。添付されているオブザーバーに、基礎となるデータが変更され、データセットを反映しているビューが更新されることを通知します。実装する前にこれを理解する... notifyDatasetChanged()は、アダプタデータ内のデータが更新されたか、または単に変更されたときに使用されます。アダプタに関連付けられたコレクションが変更されます。創造でそれを呼び出す必要はありません – Pragnani

+0

私は創造でそれを呼び出すことはなかったので、私は多くのコードをカットした、ちょうどここでそのように見えた。 – GrilledCheese

答えて

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(); 
} 
} 

をカスタムアダプタのインスタンスを作成しますので、あなたはあなたが好きなどこにでもそれを使用することができ、その後、あなたは

を使用する必要はありません
adapter.notifyDatasetChanged(); 
+0

ありがとう、これは非常に簡単な説明であり、完全に動作します(私はnotifyDataSetChanged();でSetを大文字にしなければならなかった)他の人が気付いたように – GrilledCheese

+0

Ahhはい、私はそれを見落としました:)しかし、あなたのリストが既にロードされている場合は、あなたの状況でそれを使用してください。 – yahya

1

いけないnotifyDataSetChanged()を呼び出します。メソッドを作成中に。

は、場合にのみ、あなたのlistViewScoreの内容が変化する...それを呼び出すと

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

listView.getAdapter().notifyDatasetChanged(); 

置き換えること、時間

でそれを使用して魔法を見て...

ありがとうございました。あなたのlistScoreアレイがすでにロードされている場合

関連する問題