lundi 16 avril 2018

Why does my gridiew randomly place my textview named total in different cells as the total textview while scrolling

I have an app I'm making for my mother it would be my very first app . That being said I feel like im very close at being a android expert at that at a very low level. Novice if you will . Uhm I really need an answer here I already google for two days. I cant understand how this works I get the getView () and other adapters and how it works along side with the view holder pattern but it may sound elementary to more advanced programmers here . But why does this not work . On each cell of my grid view appears three text views a quantity textview that gets incremented and decremented by two buttons with a plus sign and minus sign a textview that displays the product of the cell such as lipstick , lip gloss, ,mascara , etc, and the total textview that changes when the buttons are clicked based on the quantity that was incremented. So basically it's an app that someone can use to find the total value, tax, and product price of a product on a grid cell when clicked. Problem is I researched seen videos on YouTube from Google that shows how to use List view and grid view on articles on the Internet . May code speaks for itself I would hope so . But yes why does my click place my total textview inside a other cell when I click as it scrolls that is the key problem when i scroll meant to say

I also tried several ways of getting this to work I would appreciate straight forward answers I tried github examples for the viewholder such as using a constructor inside the viewholder class to initialize the widgets using the findviewbyid.

Here is code.

package com.mycompany.myapp;
import android.widget.*;
import android.view.*;
import java.util.*;
import java.util.zip.*;
import android.content.*;
import android.view.View.*;
import java.text.*;
import android.widget.Toolbar.*;
import android.location.*;
import android.graphics.*;
import android.content.res.*;

public class GridViewAdapter extends BaseAdapter
{
    private Context context;
    private HashMap<String, Double> map;
    private NumberFormat format;

    public GridViewAdapter(Context c){
        context = c;
        format = NumberFormat.getCurrencyInstance();
        map = new HashMap<String, Double>();
        map.put(items_arr[0], 10.00);
        map.put(items_arr[1], 20.00);
        map.put(items_arr[2], 30.00);
        map.put(items_arr[3], 4.12);
        map.put(items_arr[4], 3.00);
        map.put(items_arr[5], 2.00);
        map.put(items_arr[6], 3.00);
        map.put(items_arr[7], 120.12);
        map.put(items_arr[8], 121.30);
        map.put(items_arr[9], 101.00);
        map.put(items_arr[10], 210.00);
        map.put(items_arr[11], 301.00);
        map.put(items_arr[12], 132.12);
        map.put(items_arr[13], 150.00);
        map.put(items_arr[14], 290.00);
        map.put(items_arr[15], 308.00);
        map.put(items_arr[16], 129.12);
        map.put(items_arr[17], 126.30);
    }
    static class ViewHolder {
        public TextView label;
        public TextView qty;
        public TextView total;
        public Button addbutton;
        public Button minusbutton;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent)
    {
        ViewHolder holder;

        if(convertView == null){

            convertView =  LayoutInflater.from(context).inflate(R.layout.grid_items, parent, false);
            holder = new ViewHolder();
            holder.label = (TextView) convertView.findViewById(R.id.grid_itemsTextView_label);
            holder.qty = (TextView) convertView.findViewById(R.id.grid_itemsTextView_qty);
            holder.total = (TextView) convertView.findViewById(R.id.grid_itemsTextView_total);
            holder.addbutton = (Button) convertView.findViewById(R.id.grid_itemsButton_add);
            holder.minusbutton = (Button) convertView.findViewById(R.id.grid_itemsButton_minus);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        // TODO: Implement todo method
        holder.label.setText(items_arr[pos]);
        holder.label.setTypeface(Typeface.SERIF);
        holder.total.setTypeface(Typeface.SERIF);

        initInstancess(holder, pos);

        return convertView;
    }

    private void initInstancess(final ViewHolder holder, final int position){

        holder.addbutton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                    int num = Integer.parseInt(holder.qty.getText().toString()) + 1;
                    holder.qty.setText(num + "");
                    holder.total.setText("Total=" + String.valueOf(format.format(getTotal(position) * num)) + " " + "price:" + String.valueOf(format.format(getProductPrice(position) * num) + " " + "tax:" + " " + String.valueOf(format.format(getTax(position) * num))));
                    holder.total.setPadding(20,20,20,26);
                    holder.minusbutton.setBackgroundResource(R.color.blue);
                    holder.addbutton.setBackgroundColor(Color.WHITE);

                }
            });

        holder.minusbutton.setOnClickListener(new OnClickListener(){
                public void onClick(View v){

                    int num = 0;
                    if(Integer.parseInt(holder.qty.getText().toString()) == 0 && holder.qty.getText().toString() != null){
                        holder.qty.setText("0");
                    } else {
                        num = Integer.parseInt(holder.qty.getText().toString()) - 1;
                       holder.qty.setText(num + "");
                    }
                    holder.total.setText("Total=" + String.valueOf(format.format(getTotal(position) * num)) +  " " + "price:" + String.valueOf(format.format(getProductPrice(position) * num) + " " + "tax:" + " " + String.valueOf(format.format(getTax(position) * num))));
                    holder.total.setPadding(20,20,20,26);
                    holder.addbutton.setBackgroundResource(R.color.blue);
                    holder.minusbutton.setBackgroundColor(Color.WHITE);

                }
            });
    }

    @Override
    public long getItemId(int p1)
    {
        // TODO: Implement this method
        return 0;
    }

    @Override
    public Object getItem(int p1)
    {
        // TODO: Implement this method
        return null;
    }

    @Override
    public int getCount()
    {
        // TODO: Implement this method
        return map.values().size();
    }

    private double getTax(int pos){
        double tax = 0;
        tax = getProductPrice(pos) * (0.095 % 100.00);
        return tax;
    }

    private double getProductPrice(int pos){
        return map.get(items_arr[pos]);
    }

    private double getTotal(int pos){
        return getTax(pos)  + getProductPrice(pos);
    }

    private String[] items_arr = new String[] {"lip stick", "lip gloss","mascera", "beauty tools", "Acne Proof", "Eye Shadow" , "Eye Shadow 2" , "Mircle Set" , "Brush", "lip stick 2", "lip gloss 2","mascera 2", "beauty tools 2", "Acne Proof 2", "Eye Shadow 3" , "Eye Shadow 4" , "Mircle Set 2" , "Brush 2"};

}




Aucun commentaire:

Enregistrer un commentaire