0
何らかの理由で、同じアイテムを2回押すまで、変数mItemIndex
はその値を更新しません。アイテムがアダプタで2回押し込まれない限りインデックスは更新されません
しかし、私はmItemIndex
の両方のインスタンスをログに記録したとき、異なる値を記録しました。ここで何が間違っていますか?私は一日中これをデバッグしてきましたが、適切な解決策を見つけることができません。
package com.app.burgerking.menu.category.product;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.ParseException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.app.burgerking.R;
import com.app.burgerking.layout.FontHandler;
import com.app.burgerking.models.ModifierGroup;
import com.app.burgerking.models.ModifierItem;
import android.content.SharedPreferences;
/*** Reference to Pass TextView to Adapter:
http://stackoverflow.com/questions/9785523/on-button-click-how-to-increment-counter-and-show-in-textview
***/
public class ModifierGridViewAdapter extends BaseAdapter {
private Context mContext;
private List<ModifierItem> mItems;
private Typeface berthold;
private String mModGroupId;
private TextView tv_totalPrice;
private TextView tv_totalPrice2;
private int mItemIndex = -1;
private TextView tv_availableOptionsCount;
private ModifierGroup mModGroup;
private NumberFormat nf;
// Gregg's Code
private static final String ACTIVITY = "languageStorage";
private static String savedLanguage = "";
private static SharedPreferences spLanguage;
// End
public ModifierGridViewAdapter(Context context) {
mContext = context;
mItems = new ArrayList<ModifierItem>();
berthold = FontHandler.getTypefaceBertholdCondensed();
nf = NumberFormat.getInstance(mContext.getResources().getConfiguration().locale);
// Gregg's Code
spLanguage = mContext.getSharedPreferences(ACTIVITY, Context.MODE_PRIVATE);
// End
}
@Override
public int getItemViewType(int position) {
int type;
if (mModGroup.getMax() <= 1) {
// Load Second Layout (Layout with only touch events)
type = 1;
} else {
// Load First Layout (Layout with Increment/Decrement buttons)
type = 0;
}
return type;
}
public void setTotalPriceTextView(TextView textViewFromActivity) {
this.tv_totalPrice = textViewFromActivity;
}
public void setTotalPriceTextView2(TextView textViewFromActivity) {
this.tv_totalPrice2 = textViewFromActivity;
}
public void setAvailableOptionsCount(TextView textViewFromActivity) {
this.tv_availableOptionsCount = textViewFromActivity;
}
public void setModifierGroup(ModifierGroup modGroupFromActivity) {
this.mModGroup = modGroupFromActivity;
}
public void setItems(List<ModifierItem> items, String id) {
mItems = items;
mModGroupId = id;
}
public int countSelected() {
int count = 0;
for (int i = 0; i < mItems.size(); i++) {
if (mItems.get(i).isSelected())
count++;
}
return count;
}
private List<ModifierItem> getSelectedModifiers() {
List<ModifierItem> list = new ArrayList<ModifierItem>();
for (int i = 0; i < mItems.size(); i++) {
ModifierItem item = mItems.get(i);
if (item.isSelected()) {
item.setGroupId(mModGroupId);
list.add(item);
}
}
return list;
}
public ModifierGroup getModifierGroup() {
String id = mModGroupId;
int min = -999;
int max = -999;
int free = -999;
String name = null;
List<ModifierItem> items = getSelectedModifiers();
return new ModifierGroup(id, min, max, free, name, items);
}
public double getTotalPrice() {
double ret = 0.0;
for (ModifierItem item : mItems) {
ret += (item.getPrice() * item.getQuantity());
}
return ret;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public ModifierItem getItem(int pos) {
return mItems.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
public void highlightSelected(ViewHolder holder, final int position) {
getItem(position).setQuantity(getItem(position).getQuantity() + 1);
double convertedTotalPrice = 0.0;
double convertedTotalPrice2 = 0.0;
try {
convertedTotalPrice = nf.parse(tv_totalPrice.getText().toString()
.substring(0, tv_totalPrice.getText().toString().indexOf(" "))).doubleValue();
convertedTotalPrice2 = nf.parse(tv_totalPrice2.getText().toString()
.substring(0, tv_totalPrice2.getText().toString().indexOf(" "))).doubleValue();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
convertedTotalPrice += getItem(position).getPrice();
convertedTotalPrice2 += getItem(position).getPrice();
tv_totalPrice.setText(String.format("%.2f " + mContext.getString(R.string.sar_string),
convertedTotalPrice));
tv_totalPrice2.setText(String.format("%.2f " + mContext.getString(R.string.sar_string),
convertedTotalPrice2));
holder.container.setBackgroundResource(R.drawable.modifier_button_toggled);
holder.name.setTextColor(Color.parseColor("#ffffff"));
holder.img.setImageResource(R.drawable.checkout_check);
holder.price.setTextColor(Color.parseColor("#ffffff"));
}
public void unhighlightSelected(ViewHolder holder, final int position) {
decrementAvailableOptions(tv_availableOptionsCount);
getItem(position).setQuantity(getItem(position).getQuantity() - 1);
double convertedTotalPrice = 0.0;
double convertedTotalPrice2 = 0.0;
try {
convertedTotalPrice = nf.parse(tv_totalPrice.getText().toString()
.substring(0, tv_totalPrice.getText().toString().indexOf(" "))).doubleValue();
convertedTotalPrice2 = nf.parse(tv_totalPrice2.getText().toString()
.substring(0, tv_totalPrice2.getText().toString().indexOf(" "))).doubleValue();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
convertedTotalPrice -= getItem(position).getPrice();
convertedTotalPrice2 -= getItem(position).getPrice();
tv_totalPrice.setText(String.format("%.2f " + mContext.getString(R.string.sar_string), convertedTotalPrice));
tv_totalPrice2.setText(String.format("%.2f " + mContext.getString(R.string.sar_string), convertedTotalPrice2));
holder.container.setBackgroundResource(R.drawable.modifier_button_untoggled);
holder.name.setTextColor(Color.parseColor("#777777"));
holder.img.setImageResource(R.drawable.checkout_add);
holder.price.setTextColor(Color.parseColor("#777777"));
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final ViewHolder holder;
savedLanguage = new String(spLanguage.getString("language", ""));
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case 1: // Modifier Group has only less than or equal to one Available Option
convertView = inflater.inflate(R.layout.modifier_item, parent, false);
holder = new ViewHolder();
holder.container = (RelativeLayout) convertView.findViewById(R.id.modifier_item_container);
holder.img = (ImageView) convertView.findViewById(R.id.modifier_item_image);
holder.name = (TextView) convertView.findViewById(R.id.modifier_item_name);
holder.price = (TextView) convertView.findViewById(R.id.modifier_item_price);
convertView.setTag(holder);
convertView.setTag(R.id.modifier_item_container, holder.container);
convertView.setTag(R.id.modifier_item_image, holder.img);
convertView.setTag(R.id.modifier_item_name, holder.name);
convertView.setTag(R.id.modifier_item_price, holder.price);
if (getItem(position).getQuantity() > 0) {
mItemIndex = position;
highlightSelected(holder, position);
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.d("INDEX", Integer.toString(mItemIndex));
// Case 1: The user deselects the item he/she selected.
if (getItem(position).getQuantity() > 0) {
mItemIndex = -1;
Log.d("INDEX 2", Integer.toString(mItemIndex));
unhighlightSelected(holder, position);
} else {
}
}
});
break;
case 0: // Modifier Group has more than one Available Option
convertView = inflater.inflate(R.layout.modifier_item_2, parent, false);
holder = new ViewHolder();
holder.container = (RelativeLayout) convertView.findViewById(R.id.modifier_item_container_2);
holder.incrementImg = (ImageView) convertView.findViewById(R.id.modifier_item_increment_quantity);
holder.quantity = (TextView) convertView.findViewById(R.id.modifier_item_quantity);
holder.decrementImg = (ImageView) convertView.findViewById(R.id.modifier_item_decrement_quantity);
holder.name = (TextView) convertView.findViewById(R.id.modifier_item_name_2);
holder.price = (TextView) convertView.findViewById(R.id.modifier_item_price_2);
convertView.setTag(holder);
convertView.setTag(R.id.modifier_item_container_2, holder.container);
convertView.setTag(R.id.modifier_item_increment_quantity, holder.incrementImg);
convertView.setTag(R.id.modifier_item_quantity, holder.quantity);
convertView.setTag(R.id.modifier_item_decrement_quantity, holder.decrementImg);
convertView.setTag(R.id.modifier_item_name_2, holder.name);
convertView.setTag(R.id.modifier_item_price_2, holder.price);
holder.quantity.setText(Integer.toString(getItem(position).getQuantity()));
holder.quantity.setTypeface(berthold);
holder.incrementImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isAvailableCountBelowZero(tv_availableOptionsCount)) {
displayErrorMessage();
return;
}
int acquiredQuantity = Integer.parseInt(holder.quantity.getText().toString());
acquiredQuantity += 1;
getItem(position).setQuantity(acquiredQuantity);
double convertedTotalPrice = 0.0;
double convertedTotalPrice2 = 0.0;
try {
convertedTotalPrice = nf.parse(tv_totalPrice.getText().toString()
.substring(0, tv_totalPrice.getText().toString().indexOf(" "))).doubleValue();
convertedTotalPrice2 = nf.parse(tv_totalPrice2.getText().toString()
.substring(0, tv_totalPrice2.getText().toString().indexOf(" "))).doubleValue();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
convertedTotalPrice += getItem(position).getPrice();
convertedTotalPrice2 += getItem(position).getPrice();
tv_totalPrice.setText(String.format("%.2f " + mContext.getString(R.string.sar_string),
convertedTotalPrice));
tv_totalPrice2.setText(String.format("%.2f " + mContext.getString(R.string.sar_string),
convertedTotalPrice2));
holder.quantity.setText(Integer.toString(acquiredQuantity));
}
});
holder.decrementImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int acquiredQuantity = Integer.parseInt(holder.quantity.getText().toString());
if (acquiredQuantity - 1 < 0) {acquiredQuantity = 0;}
else {
decrementAvailableOptions(tv_availableOptionsCount);
acquiredQuantity -= 1;
getItem(position).setQuantity(acquiredQuantity);
double convertedTotalPrice = 0.0;
double convertedTotalPrice2 = 0.0;
try {
convertedTotalPrice = nf.parse(tv_totalPrice.getText().toString()
.substring(0, tv_totalPrice.getText().toString().indexOf(" "))).doubleValue();
convertedTotalPrice2 = nf.parse(tv_totalPrice2.getText().toString()
.substring(0, tv_totalPrice2.getText().toString().indexOf(" "))).doubleValue();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
convertedTotalPrice -= getItem(position).getPrice();
convertedTotalPrice2 -= getItem(position).getPrice();
tv_totalPrice.setText(String.format("%.2f " + mContext.getString(R.string.sar_string),
convertedTotalPrice));
tv_totalPrice2.setText(String.format("%.2f " + mContext.getString(R.string.sar_string),
convertedTotalPrice2));
}
holder.quantity.setText(Integer.toString(acquiredQuantity));
}
});
break;
default:
convertView = inflater.inflate(R.layout.modifier_item, parent, false);
holder = new ViewHolder();
holder.container = (RelativeLayout) convertView.findViewById(R.id.modifier_item_container);
holder.img = (ImageView) convertView.findViewById(R.id.modifier_item_image);
holder.name = (TextView) convertView.findViewById(R.id.modifier_item_name);
holder.price = (TextView) convertView.findViewById(R.id.modifier_item_price);
convertView.setTag(holder);
convertView.setTag(R.id.modifier_item_container, holder.container);
convertView.setTag(R.id.modifier_item_image, holder.img);
convertView.setTag(R.id.modifier_item_name, holder.name);
convertView.setTag(R.id.modifier_item_price, holder.price);
break;
}
}
else {
holder = (ViewHolder) convertView.getTag();
}
if (savedLanguage.equals("ar")) {
holder.name.setText(getItem(position).getAltName().toUpperCase());
} else {
holder.name.setText(getItem(position).getName().toUpperCase());
}
holder.price.setText(String.format("%.2f " + mContext.getString(R.string.sar_string),
getItem(position).getPrice()));
holder.name.setTypeface(berthold);
holder.price.setTypeface(berthold);
return convertView;
}
public void decrementAvailableOptions(TextView tv_count) {
int availableCount = Integer.parseInt(tv_count.getText().toString());
if (availableCount + 1 > mModGroup.getMax()) {
availableCount = mModGroup.getMax();
} else {
availableCount += 1;
if ((mModGroup.getSelectedCount() - 1) < 0) {
mModGroup.setSelectedCount(0);
} else {
mModGroup.setSelectedCount(mModGroup.getSelectedCount() - 1);
}
}
tv_count.setText(Integer.toString(availableCount));
}
public boolean isAvailableCountBelowZero(TextView tv_count) {
boolean isBelowZero = false;
int availableCount = Integer.parseInt(tv_count.getText().toString());
if (availableCount - 1 < 0) {
isBelowZero = true;
}
else {
mModGroup.setSelectedCount(mModGroup.getSelectedCount() + 1);
tv_count.setText(Integer.toString(availableCount - 1));
}
return isBelowZero;
}
public void displayErrorMessage() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Add-On Limit");
builder.setMessage("You have exceeded the maximum number of items you can add.");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
private class ViewHolder {
RelativeLayout container;
ImageView img;
ImageView incrementImg;
TextView quantity;
ImageView decrementImg;
TextView name;
TextView price;
}
}
mItemIndexの目的は、数量が0より大きいItemのインデックスを決定することです。 – Gregg