-3
私はアンドロイドの新人です。だから私は2つのボタン、基本的なリストビューを1つ増分し、もう1つは、その行のテキストビューを減らすために持っています。いずれかのボタンをクリックすると、目的のテキストビュー(行0)は変更されますが、画面上でさえも行(行5)でない別の行も変更されます。リストビュー内の特定の行を変更すると、別の行が変更を反映します。
public class ItemAdapter extends ArrayAdapter<Item> {
public ItemAdapter(Context context, ArrayList<Item> items) {
super(context, 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_list_view, parent, false);
}
// Lookup view for data population
TextView tvItem = (TextView) convertView.findViewById(R.id.item_lv_name);
ImageView ivItemPicture = (ImageView) convertView.findViewById(R.id.item_lv_item_pic);
final TextView tvAmount = (TextView)convertView.findViewById(R.id.item_lv_amount);
// Populate the data into the template view using the data object
tvItem.setText(item.getName());
ivItemPicture.setImageResource(item.getImageResId());
//incremt textview
ImageButton ibtnPlus = (ImageButton)convertView.findViewById(R.id.item_lv_plus);
ibtnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int value = Integer.parseInt(tvAmount.getText().toString());
if(value >= 0){
value = value + 1;
tvAmount.setText(Integer.toString(value));
}
}
});
//decrement textview
ImageButton ibtnMinus = (ImageButton)convertView.findViewById(R.id.item_lv_btn_minus);
ibtnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int value = Integer.parseInt((tvAmount.getText().toString()));
if (value > 0){
value = value - 1;
tvAmount.setText(Integer.toString(value));
}
}
});
// Return the completed view to render on screen
return convertView;
}
}
'ArrayList'を使用してcountを保持します。その特定のインデックスのカウントonClick()イベントをインクリメントします。そしてそのリストのカウントを使って 'tvAmount'のテキストを設定します。これについてもっと疑問がある場合はお気軽にお問い合わせください。私は喜んでお手伝いします。 Patelさんのように –
さんは、リスト内のcountのステータスを更新する必要があると言いました。また、リスト項目を使用してgetViewでデータを設定します。 – Vinodh
@bioioこの理由はtvAmountに依存しているため、このビューはスクロールするたびにリサイクルされ、再び使用されます。ビューは再作成されず、再利用されます。それで、あなたが他のものが言ったように、いくつかのコンテナのテキストビューに表示したい値を保持してくださいArrayListまたはとにかくu ** Item **モデルは量のフィールドを保持し、そのアイテムから選択してセットします – Raghavendra