How to remove or hide MarkerView in MPAndroidChart? - android

I did a bar chart with MPAndroidChart. I did a custom marker view class to view the values of the bar.
I would remove or hide the view after some seconds. It should work as a toast. But I cannot do it.
This is a problem first of all because after I touched the chart for the first time, it becomes visible and it disappears only if i find the right tapped condition.
There is someone who knows how solve this problem?
In my code I have:
mChart.invalidate();
mv = new CustomMarkerView(getContext(), R.layout.alert_chart, position, jsonResponse);
mChart.setMarker(mv);
And my CustomMarkerView is
public class CustomMarkerViewextends MarkerView {
public TextView correct;
public TextView wrong ;
public int position;
public JSONArray jsonArray;
public CustomMarkerView(Context context, int layoutResource, int pos, JSONArray json) {
super(context, layoutResource);
correct = (TextView) findViewById(R.id.correct);
wrong = (TextView) findViewById(R.id.wrong);
position = pos;
jsonArray = json;
}
#Override
public void refreshContent(Entry e, Highlight highlight) {
switch (position){
case 1:
try {
correct.setText(getContext().getString(R.string.correct_alert) + " " + jsonArray.getJSONObject((int)(e.getX())).getString("TP"));
wrong.setText(getContext().getString(R.string.wrong_alert) + " " + jsonArray.getJSONObject((int)(e.getX())).getString("FP"));
} catch (JSONException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
break;
case 2:
try {
correct.setText(getContext().getString(R.string.tpr_alert) + " " + Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("TPR"))*100.0)/100.0);
wrong.setText(getContext().getString(R.string.msr_alert) + " " + Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("MSR"))*100.0)/100.0);
} catch (JSONException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
break;
case 3:
try {
correct.setText(getContext().getString(R.string.nmean_alert) + " " +Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("n_mean"))*100.0)/100.0);
wrong.setText(getContext().getString(R.string.nmax_alert) + " " +Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("n_max"))*100.0)/100.0);
} catch (JSONException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
break;
}
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
#Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth()/2), -getHeight());
}
return mOffset;
}
}

I know, it's a bit late, but:
mChart.highlightValue(null);
Worked for me

Set an onTouchListener to your chart and in the MotionEvent ACTION_UP set the highlight value to null. Code :
chart.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP: {
chart.highlightValue(null);
break;
}
}
return false;
}
});

Related

How to fix memory leaks for view created and added dynamically by removing all views?

I have a custom class to generate dynamically view by my generate() method but when i call finish() method in my activity gc doesn't work and memory up to 10 MB for every activity call (using checking on Profiler), I need to fix it with manual delete view from memory with gc, I know gc do it when i have no strong reference to my views and i followed this rules and save all my views to collection as WeakHashMap but gc doesn't deallocation for my views:
public class CustomQuestionAdapter {
public ArrayList<Question> items;
private Context mActivity;
private boolean isDisabled = false;
private String TAG = "CUSTOM_QUESTION_ADAPTER";
private OnProgressViewChanged onProgressViewChanged;
private WeakHashMap<String, View> boxReferences = new WeakHashMap<>();
private SparseArray<EditTextPlus> textViewsReferences = new SparseArray<>();
public CustomQuestionAdapter(Context context) {
this.mActivity = context;
}
public void setItems(ArrayList<Question> items) {
this.items = items;
}
public void generate(int index) {
int total = items.size();
generateBoxes(index);
this.onProgressViewChanged.onChanged((int) ((index * 1.0 / total) * 100));
if ( index < total - 1){
this.onProgressViewChanged.onFinish(boxReferences.get(items.get(index).getQId().toString()), false);
}else{
this.onProgressViewChanged.onChanged(100);
this.onProgressViewChanged.onFinish(boxReferences.get(items.get(index).getQId().toString()), true);
}
}
public void removeAllView(ViewGroup v){
for (View view: boxReferences.values()){
try{
view.setVisibility(View.VISIBLE);
((ViewGroup) view).removeAllViews();
Log.d(TAG, "removeAllView: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ removed ");
}catch (Exception e){
Log.d(TAG, "removeAllView: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ occured error" );
}
}
if (v != null){
v.removeAllViews();
}
boxReferences = null;
textViewsReferences = null;
items.clear();
}
public void setDisabled(boolean isDisabled) {
this.isDisabled = isDisabled;
}
#Nullable
private View generateBoxes(final int position) {
final Question item = items.get(position);
final HashMap<String, String> options = item.getOptionsHashMap();
item.setQText(String.format("(%d) %s", position + 1, item.getQText()));
if (item.getQType().equals("Text")) {
// final View layoutView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_question_text, null, false);
View layoutView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_question_text, null, false);
try {
((TextViewPlus) layoutView.findViewById(R.id.question)).setText(String.format("%s%s", item.getQText(), Boolean.valueOf(options.get("required")) ? " (الزامی)" : ""));
} catch (Exception e) {
e.printStackTrace();
}
EditTextPlus answerText = layoutView.findViewById(R.id.answer);
textViewsReferences.put(position, answerText);
answerText.setText(item.getAnswer());
if (isDisabled)
answerText.setEnabled(false);
layoutView.setVisibility(item.getShouldHide() ? View.GONE : View.VISIBLE);
boxReferences.put(item.getQId().toString(), layoutView);
return layoutView;
} else if (item.getQType().equals("Checkbox")) {
View layoutView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_question_radio, null, false);
LinearLayout optionsLayout = layoutView.findViewById(R.id.options);
try {
((TextViewPlus) layoutView.findViewById(R.id.question)).setText(String.format("%s%s", item.getQText(), Boolean.valueOf(options.get("required")) ? " (الزامی)" : ""));
} catch (Exception e) {
e.printStackTrace();
}
// new Thread(new Runnable() {
// #Override
// public void run() {
optionsLayout.removeAllViews();
final List<CheckBox> checkBoxes = new ArrayList<>();
for (int i = 0; i < item.getFields().size(); i++) {
Field field = item.getFields().get(i);
View view = LayoutInflater.from(mActivity).inflate(R.layout.template_checkbox, null);
CheckBox checkBox = view.findViewById(R.id.radio);
TextViewPlus titleText = view.findViewById(R.id.title);
if (isDisabled)
checkBox.setEnabled(false);
checkBox.setChecked(item.getAnswer().contains(field.getName() + "|"));
checkBox.setTag(field.getName());
titleText.setText(field.getTitle());
optionsLayout.addView(view);
checkBoxes.add(checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
StringBuilder answer = new StringBuilder();
for (CheckBox checkBox1 : checkBoxes) {
if (checkBox1.isChecked()) {
answer.append(checkBox1.getTag()).append("|");
}
}
// answers.set(position, answer);
item.setAnswer(answer.toString());
}
});
}
// }
// }).start();
boxReferences.put(item.getQId().toString(), layoutView);
layoutView.setVisibility(item.getShouldHide() ? View.GONE : View.VISIBLE);
return layoutView;
} else if (item.getQType().equals("Input")) {
View layoutView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_question_input, null, false);
try {
((TextViewPlus) layoutView.findViewById(R.id.question)).setText(String.format("%s%s", item.getQText(), Boolean.valueOf(options.get("required")) ? " (الزامی)" : ""));
EditTextPlus answerText = layoutView.findViewById(R.id.answer);
switch (options.get("type")) {
case "text": {
answerText.setInputType(InputType.TYPE_CLASS_TEXT);
break;
}
case "number": {
answerText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
break;
}
case "date": {
answerText.setInputType(InputType.TYPE_CLASS_DATETIME);
break;
}
}
textViewsReferences.put(position, answerText);
answerText.setText(item.getAnswer());
if (isDisabled)
answerText.setEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
boxReferences.put(item.getQId().toString(), layoutView);
layoutView.setVisibility(item.getShouldHide() ? View.GONE : View.VISIBLE);
return layoutView;
} else if (item.getQType().equals("Radio Button")) {
View layoutView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_question_radio_with_group, null, false);
LinearLayout textGroup = layoutView.findViewById(R.id.textGroup);
RadioGroup radioGroup = layoutView.findViewById(R.id.radioGroup);
try {
((TextViewPlus) layoutView.findViewById(R.id.question)).setText(String.format("%s%s", item.getQText(), Boolean.valueOf(options.get("required")) ? " (الزامی)" : ""));
} catch (Exception e) {
e.printStackTrace();
}
textGroup.removeAllViews();
radioGroup.removeAllViews();
for (int i = 0; i < item.getFields().size(); i++) {
final Field field = item.getFields().get(i);
final RadioButton radioButton = new RadioButton(mActivity);
radioButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 70));
radioButton.setGravity(Gravity.CENTER);
radioButton.setChecked(false);
radioButton.setId(View.generateViewId());
TextViewPlus titleText = new TextViewPlus(mActivity) {{
setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 70) {{
rightMargin = 10;
}});
setEllipsize(TextUtils.TruncateAt.MARQUEE);
setFont(mActivity, "fonts/IRANYekanWebLight.ttf");
setTextSize(Dimension.SP, 14f);
setGravity(Gravity.CENTER | Gravity.RIGHT);
setTextColor(mActivity.getResources().getColor(R.color.gray));
}};
radioButton.setChecked(item.getAnswer().equals(field.getName()));
radioButton.setTag(field.getName());
if (isDisabled) {
radioButton.setEnabled(false);
}
titleText.setText(field.getTitle());
textGroup.addView(titleText);
radioGroup.addView(radioButton);
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
item.setAnswer(field.getName());
// items.set(position, item);
}
//
}
});
}
if (isDisabled) {
radioGroup.setEnabled(false);
}
// }
// }).start();
boxReferences.put(item.getQId().toString(), layoutView);
layoutView.setVisibility(item.getShouldHide() ? View.GONE : View.VISIBLE);
return layoutView;
} else if (item.getQType().equals("OPTCheckbox")) {
View layoutView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_question_radio, null, false);
LinearLayout optionsLayout = layoutView.findViewById(R.id.options);
// new Thread(new Runnable() {
// #Override
// public void run() {
try {
((TextViewPlus) layoutView.findViewById(R.id.question)).setText(String.format("%s%s", item.getQText(), Boolean.valueOf(options.get("required")) ? " (الزامی)" : ""));
} catch (Exception e) {
e.printStackTrace();
}
optionsLayout.removeAllViews();
Field field = item.getFields().get(0);
View view = LayoutInflater.from(mActivity).inflate(R.layout.template_checkbox, null);
final CheckBox checkBox = view.findViewById(R.id.radio);
TextViewPlus titleText = view.findViewById(R.id.title);
if (isDisabled)
checkBox.setEnabled(false);
Log.e("~~~~~~~", item.getAnswer());
if (item.getAnswer().contains(field.getName() + "|")) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
checkBox.setTag(field.getName());
checkBox.setId((int) 123);
titleText.setText(field.getTitle());
optionsLayout.addView(view);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
item.setAnswer(b ? checkBox.getTag() + "|" : "");
// items.set(position, item);
try {
JSONArray toToggleItems = new JSONArray(item.getOptionsHashMap().get("hidden_ls"));
for (int i = 0; i < toToggleItems.length(); i++) {
for (int j = 0; j < items.size(); j++) {
if (items.get(j).getQId().toString().equals(toToggleItems.getString(i))) {
items.get(j).setShouldHide(!b);
try {
boxReferences.get(items.get(j).getQId().toString()).setVisibility(!b ? View.GONE : View.VISIBLE);
} catch (Exception ignored) {
}
if (!b && items.get(j).getOptionsHashMap().containsKey("hidden_ls")) {
try {
((CheckBox) boxReferences.get(items.get(j).getQId().toString()).findViewById((int) 123)).setChecked(false);
} catch (Exception ignored) {
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
// }
// }).start();
boxReferences.put(item.getQId().toString(), layoutView);
layoutView.setVisibility(item.getShouldHide() ? View.GONE : View.VISIBLE);
return layoutView;
}
return null;
}
public void prepareItemsForValidation(int position) {
try {
items.get(position).setAnswer(textViewsReferences.get(position).getText().toString());
} catch (Exception ignored) {
}
}
public int getItemViewType(int position) {
final Question item = items.get(position);
// Log.e(TAG, "type:"+item.type);
switch (item.getQType()) {
case "Text":
return VIEW_TYPE_TEXT;
case "Checkbox":
return VIEW_TYPE_CHECKBOX;
case "Input":
return VIEW_TYPE_INPUT;
case "Radio Button":
return VIEW_TYPE_RADIO;
case "OPTCheckbox":
return VIEW_TYPE_OPTCHECKBOX;
}
return VIEW_TYPE_TEXT;
// return super.getItemViewType(position);
}
public void setOnProgressViewChanged(OnProgressViewChanged onProgressViewChanged) {
this.onProgressViewChanged = onProgressViewChanged;
}
public interface OnProgressViewChanged {
void onChanged(int progress);
void onFinish(View view, Boolean lastIndex);
}
}
Even in activity i called removeAll() method in destroy() method :
#Override
protected void onDestroy() {
adapter.removeAllView(insideContainer);
System.gc();
super.onDestroy();
}
I found that every time we call the gc, the gc doesn't do its job. In fact, by following a number of things, such as not keeping a strong reference from the View in memory, the gc will be able to remove the heap while the heap is full. So because WeakHashMap has been used, gc will delete it on time. Otherwise, it wouldn't have happened if I had used HashMap.

When I remove an item, and then I add a new one, the information of removed item remains

I use the recycler view for add and remove the blocks with the edittext,
but when I remove a block, and then add a new one, the information from the removed one returns to a new one, but I need a clean block to be added
delete.setOnClickListener(v -> {
int position = getAdapterPosition();
try {
connector.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, connector.size());
notifyDataSetChanged();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
});
add.setOnClickListener(v -> {
int position = getAdapterPosition();
try {
connector.add(position + 1, "");
notifyItemInserted(position + 1);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
});
ViewHolder full:
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView delete;
TextInputEditText typeOfConnector;
#SuppressLint("CutPasteId")
public ViewHolder(View itemView) {
super(itemView);
delete = itemView.findViewById(R.id.iv_garbage);
typeOfConnector = itemView.findViewById(R.id.type_of_connector_et);
delete.setOnClickListener(v -> {
int position = getAdapterPosition();
try {
connector.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, connector.size());
notifyDataSetChanged();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
});
add.setOnClickListener(v -> {
int position = getAdapterPosition();
try {
connector.add(position + 1, "");
notifyItemInserted(position + 1);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
});
}
}
First of all, you don't need all three commands:
notifyItemRemoved(position);
notifyItemRangeChanged(position, connector.size());
notifyDataSetChanged();
For your use-case: just notifyItemRemoved(position); will do
Now in your bindViewHolder method, you need to set the Edittext to the respective string in connector.

Dynamic Spinners item selection error

I' am creating and populating spinners dynamically,the problem is while selecting the items(eg:4th) of any spinner ,i'am getting the id of the last spinners which is created dynamically item(4th id). I want to get the corresponding id's which i was set dynamically to the spinner items.
How to get the values of each spinner correctly..
Codes are give below.
public void secondarray(JsonParser jsonParser) {
String fieldName;
ameList = new ArrayList<AmeModel>();
AmeModel ameModel = null;
try {
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
ameModel = new AmeModel();
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
fieldName = jsonParser.getCurrentName();
if ("valueid".equals(fieldName)) {
jsonParser.nextToken();
ameModel.setId((jsonParser.getText()));
} else if ("valuename".equals(fieldName)) {
jsonParser.nextToken();
ameModel.setName((jsonParser.getText()));
} else {
jsonParser.skipChildren();
}
}
ameList.add(ameModel);
}
Spinner sp=new Spinner(MainActivity.this);
AmeAdapter adapter = new AmeAdapter(this, ameList);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, ameList.get(i).getId(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
container.addView(sp);
//setContentView(linearlayout);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Any help will be appreciated thank you.
For get selected item Name:
String itemName=sp.getSelectedItem().toString(); // sp is your spinner
For get selected item position :
int position = sp.getSelectedItemPosition();
Now as per your code snippiest If you want to get Id Those you have set dynamically.
HashMap<String, String> nameId;
String itemId;
String itemName= String.valueOf(sp.getSelectedItem());
for (String s : nameId.keySet()) {
if (nameId.get(s).equals(itemName)) {
itemId= s;
}
Try to use above suggestion

Change text style / size of selected value in NumberPicker

I've got two problems to custom my numberpicker on my application. After lot of research I didn't found anything that help me.
First is that I want increase the selected text. I already have a class that extends number picker to customize the general text size, font and divider visibility, but I'm not able to increase only the selected value.
Second problems, I want to put some unicode on my text, so I'm trying to use a spannableString[] instead of String[] for the formattter, but nothing seems to work. I would use Spannable to be able to change the police font of a little part of my text (With some Regex)
Is there a way to do this ?
What I've done actually :
public class MyNumberPicker extends NumberPicker{
private Context context;
public MyNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
#Override
public void addView(View child) {
super.addView(child);
updateView(child);
}
#Override
public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
#Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
updateView(child);
}
private void updateView(View view) {
if(view instanceof EditText){
//Text size
((EditText) view).setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//Text font
Typeface robotoFont = FontCache.get("fonts/Roboto-Regular.ttf", getContext());
((EditText) view).setTypeface(robotoFont);
}
//Non visible selectors
java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (java.lang.reflect.Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.setAccessible(true);
try {
ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(
getContext(), R.color.colorTransparent
));
pf.set(this, colorDrawable);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
}
THe formatter :
npTemp.setMinValue(0);
npTemp.setMaxValue(values.length - 1);
NumberPicker.Formatter formatter = new NumberPicker.Formatter() {
#Override
public String format(int value) {
return values[value];
}
};
npTemp.setFormatter(formatter);
What I've tried to get the selected value EditText on my custom Number picker
java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (java.lang.reflect.Field pf : pickerFields) {
if (pf.getName().equals("mInputType")) {
pf.setAccessible(true);
try {
((EditText)pf).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
And to have SpannableString, I've done lot of things but the formatter only want String.
Thanks for helping

found index 0, size is 0 in android

"There i found index out of bound exception how can i solve???"
public static void addToCart() {
ArrayList<ItemData> iArr;
if (holdSelection != null) {
String categoryID = holdSelection.getCategoryID();
int position = categoriesAddedd.indexOf(holdSelection.getCategoryID());
int itemPosition =categoriesitemAddedd.indexOf(holdSelection.getItemData().get(0).getItemID());
if (!categoriesAddedd.contains(categoryID)) {
CategoryData data = new CategoryData();
data.setCategoryID(categoryID);
data.setCategoryName(holdSelection.getCategoryName());
data.setItemData(holdSelection.getItemData());
mAddedToCart.add(data);
categoriesAddedd.add(categoryID); categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext,R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
} else {
if (position>=0) {
if(itemPosition>=0){
if(holdSelection.getItemData().get(0).getScaled()){
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}
}else{
try{
CategoryData data = mAddedToCart.get(position);
iArr = data.getItemData();
iArr.add(holdSelection.getItemData().get(0));
categoriesAddedd.add(categoryID);
categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext, R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(mContext, "There is " +e,Toast.LENGTH_SHORT).show();
System.out.println("Exception : "+ e.getMessage());
e.printStackTrace();
}
}
}
}
}
}
It's impossible to help without a minimum of (structured) information (and also without a Hello, please, tank you, polite words if you prefer).
First of all, you have to use the brackets to display code when you post, so your clean code should appear as follow :
public static void addToCart() {
ArrayList iArr;
if (holdSelection != null) {
String categoryID = holdSelection.getCategoryID();
int position = categoriesAddedd.indexOf(holdSelection.getCategoryID());
int itemPosition =categoriesitemAddedd.indexOf(holdSelection.getItemData().get(0).getItemID());
if (!categoriesAddedd.contains(categoryID)) {
CategoryData data = new CategoryData();
data.setCategoryID(categoryID);
data.setCategoryName(holdSelection.getCategoryName());
data.setItemData(holdSelection.getItemData());
mAddedToCart.add(data);
categoriesAddedd.add(categoryID);
categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext,R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
} else {
if (position>=0) {
if(itemPosition>=0){
if(holdSelection.getItemData().get(0).getScaled()){
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}
}else{
try{
CategoryData data = mAddedToCart.get(position);
iArr = data.getItemData(); iArr.add(holdSelection.getItemData().get(0));
categoriesAddedd.add(categoryID);
categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext, R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{ // TODO: handle exception Toast.makeText(mContext, "There is " +e,Toast.LENGTH_SHORT).show();
System.out.println("Exception : "+ e.getMessage()); e.printStackTrace();
}
}
}
}
}
}
then my first question will be : what is holdSelection related to?
Please add more parts of your code as your log (asked by Remees).
Alex.

Categories

Resources