たくさんの数字(重み)を表示するListActivityに取り組んでいます。ListViewの特定の行の背景を変更したいのですが。これを行うために、ArrayAdapterクラスのカスタム実装を作成し、getViewメソッドをオーバーライドしました。アダプターは番号のリストを受け入れ、番号20の行の背景を黄色に設定します(簡単にするため)。
public class WeightListAdapter extends ArrayAdapter<Integer> {
private List<Integer> mWeights;
public WeightListAdapter(Context context, List<Integer> objects) {
super(context, android.R.layout.simple_list_item_1, objects);
mWeights = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
int itemWeight = mWeights.get(position);
if (itemWeight == 20) {
v.setBackgroundColor(Color.YELLOW);
}
return v;
}
}
問題は、番号20の行だけでなく、番号0の行(最初の行)も黄色になることです。これがなぜそうなのかわかりません。
getViewメソッドで何か間違ったことをしていますか(スーパーメソッドの呼び出しなど)?実装の私の理由は次のとおりです。返されるビューはすべて同じである必要があります(そのため、スーパーメソッドを呼び出しています)。if基準に適合するビューのみを変更する必要があります。
ご協力いただきありがとうございます!