2016-01-21 97 views
1

 Here is my image 動態的ListView滾動的TextView自動

改變。

我應該爲停留增量價格做些什麼?

這裏是我的適配器

public class ListAdapter extends BaseAdapter { 

public ArrayList<Integer> quantity = new ArrayList<Integer>(); 
public ArrayList<Integer> price = new ArrayList<Integer>(); 
private String[] listViewItems, prices, static_price; 
TypedArray images; 
View row = null; 

static String get_price, get_quntity; 
int g_quntity, g_price, g_minus; 

private Context context; 
CustomButtonListener customButtonListener; 

static HashMap<String, String> map = new HashMap<>(); 


public ListAdapter(Context context, String[] listViewItems, TypedArray images, String[] prices) { 
    this.context = context; 
    this.listViewItems = listViewItems; 
    this.images = images; 
    this.prices = prices; 

    for (int i = 0; i < listViewItems.length; i++) { 
    quantity.add(0); 
    price.add(0); 
    } 
} 

public void setCustomButtonListener(CustomButtonListener customButtonListner) { 
    this.customButtonListener = customButtonListner; 
} 

@Override 
public int getCount() { 
    return listViewItems.length; 
} 

@Override 
public String getItem(int position) { 
    return listViewItems[position]; 
} 


@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    final ListViewHolder listViewHolder; 
    if (convertView == null) { 
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    row = layoutInflater.inflate(R.layout.activity_custom_listview, parent, false); 
    listViewHolder = new ListViewHolder(); 
    listViewHolder.tvProductName = (TextView) row.findViewById(R.id.tvProductName); 
    listViewHolder.ivProduct = (ImageView) row.findViewById(R.id.ivproduct); 
    listViewHolder.tvPrices = (TextView) row.findViewById(R.id.tvProductPrice); 
    listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.ib_addnew); 
    listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity); 
    listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.ib_remove); 
    static_price = context.getResources().getStringArray(R.array.Price); 
    row.setTag(listViewHolder); 
    } else { 
    row = convertView; 
    listViewHolder = (ListViewHolder) convertView.getTag(); 
    } 


    listViewHolder.ivProduct.setImageResource(images.getResourceId(position, -1)); 


    try { 

    listViewHolder.edTextQuantity.setText(quantity.get(position) + ""); 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } 

    listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    if (customButtonListener != null) { 
    customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, 1); 

    quantity.set(position, quantity.get(position) + 1); 
    price.set(position, price.get(position) + 1); 

    row.getTag(position); 

    get_price = listViewHolder.tvPrices.getText().toString(); 

    g_price = Integer.valueOf(static_price[position]); 

    get_quntity = listViewHolder.edTextQuantity.getText().toString(); 
    g_quntity = Integer.valueOf(get_quntity); 

    map.put("" + listViewHolder.tvProductName.getText().toString(), " " + listViewHolder.edTextQuantity.getText().toString()); 
    listViewHolder.tvPrices.setText("" + g_price * g_quntity); 
//     Log.d("A ", "" + a); 
//     Toast.makeText(context, "A" + a, Toast.LENGTH_LONG).show(); 
//     Log.d("Position ", "" + position); 
//     System.out.println(+position + " Values " + map.values()); 
    ShowHashMapValue(); 
    listViewHolder.tvPrices.setText("" + g_price * g_quntity); 
    } 


    } 

    }); 
    listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    if (customButtonListener != null) { 
    customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, -1); 
    if (quantity.get(position) > 0) 
     quantity.set(position, quantity.get(position) - 1); 

    get_price = listViewHolder.tvPrices.getText().toString(); 
    g_minus = Integer.valueOf(get_price); 
    g_price = Integer.valueOf(static_price[position]); 
    int minus = g_minus - g_price; 
    if (minus >= g_price) { 
     listViewHolder.tvPrices.setText("" + minus); 
    } 

    map.put("" + listViewHolder.tvProductName.getText().toString(), " " + listViewHolder.edTextQuantity.getText().toString()); 
    ShowHashMapValue(); 
    } 
    } 
    }); 
    listViewHolder.tvProductName.setText(listViewItems[position]); 
    listViewHolder.tvPrices.setText(prices[position]); 

    return row; 
} 

private void ShowHashMapValue() { 

    /** 
    * get the Set Of keys from HashMap 
    */ 
    Set setOfKeys = map.keySet(); 

/** 
* get the Iterator instance from Set 
*/ 
    Iterator iterator = setOfKeys.iterator(); 

/** 
* Loop the iterator until we reach the last element of the HashMap 
*/ 
    while (iterator.hasNext()) { 
/** 
* next() method returns the next key from Iterator instance. 
* return type of next() method is Object so we need to do DownCasting to String 
*/ 
    String key = (String) iterator.next(); 

/** 
* once we know the 'key', we can get the value from the HashMap 
* by calling get() method 
*/ 
    String value = map.get(key); 

    System.out.println("Key: " + key + ", Value: " + value); 
    } 
} 
} 
+0

您需要將所有更新的數據存儲在您的列表中,然後使用adapter.notifydatasetchange d – Pavya

+0

使用回收器視圖。 onBindView()將刷新項目列表並將值保存在那裏。 – TejjD

+0

您沒有正確使用ViewHolder模式,這就是爲什麼隨着持有人中的數據在錯誤的地方使用而動態更改數據的原因。 刪除'row = convertView'。 'try-catch'是不必要的,可以通過簡單的邊界檢查來避免。 – TheSunny

回答