Şu anda, ListView'ümü güncelleştirmek için setAdapter kullanıyorum, ancak bence doğru yolu notifiyDatasetChanged() kullanmaktır ve bunu ana sınıfımda (adaptörde) çalışmak için kullanamıyorum. İşte hatadır:ListAdapter ile çalışmak içinDatasetChanged() öğesini nasıl bildiririm?
yöntem notifyDatasetChanged() Bunu yapmanın daha iyi bir yolu vardır tahmin ediyorum tipi ListAdapter
için tanımlanmamıştır - Herkes bana doğru yönde işaret edebilir?
İşte benim kod ilgili kısımları var: Burada
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
}
}
oluyor adaptör:
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()' - Sermayeye dikkat edin. Gösterdiğiniz satırda küçük bir hata var. – Squonk
Öncelikle notifyDatasetChanged'in kullanımını bilmeniz gerekir ... Ekteki gözlemcilere, temel verilerin değiştirildiğini ve veri kümesini yansıtan herhangi bir Görünümün kendisini yenilemesi gerektiğini bildirir. Bunu uygulamadan önce anlayın ... notifyDatasetChanged(), bağdaştırıcı verilerinizdeki veriler güncellendiğinde veya basitçe değiştirildiğinde kullanılır. Bu, Bağdaştırıcıyla ilişkili koleksiyonun değiştiği anlamına gelir. Yaratılışta aramaya gerek yok – Pragnani
Teşekkürler, çok fazla kodu kestim, bu yüzden yaratımda aramadım, sadece bu tarafa baktım. – GrilledCheese