自定义列表视图中的数字选择器自行更改数字

时间:2015-11-21 12:31:57

标签: android custom-adapter numberpicker

我正在开发app,其中我使用Custom ArrayAdaptor来填充我的listView。自定义行包括一个数字选择器以及其活动中的一些文本框。 现在问题部分 - > 有趣的是,当我更改特定行中的数字选择器的数量时,其他行的其他数字选择器会自动更改。我几乎在2天后一直盯着我的代码。请帮助

Custom_row.java

公共类Custom_row扩展了ArrayAdapter {

static Button ProPay;
Context context;
int newValcount=0;

public Custom_row(Context context, int resourceId,
                  List<DataItem> items) {
    super(context, resourceId, items);
    this.context = context;
}
private class ViewHolder {
    TextView Items,Cost;
    NumberPicker number;


}
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    DataItem dataItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.activity_custom_row, null);
        holder = new ViewHolder();
        holder.Items = (TextView) convertView.findViewById(R.id.tvItem);
        holder.Cost = (TextView) convertView.findViewById(R.id.tvCost);
        holder.number=(NumberPicker)convertView.findViewById(R.id.numberPicker);
        holder.number.setMaxValue(10);
        holder.number.setMinValue(0);
        holder.number.setValue(0);
        holder.number.setFocusable(false);
        ItemList.picker = holder.number;
        holder.number.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                if(newVal!=0){
                    newValcount++;
                    if(newValcount > 0)
                        ProPay.setVisibility(View.VISIBLE);
                }
                else{
                    newValcount--;
                    if(newValcount==0)
                        ProPay.setVisibility(View.INVISIBLE);

                }
            }
        });
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.Items.setText(dataItem.getItem());
    holder.Cost.setText(dataItem.getCost());
    return convertView;
}

}

ItemList.java `public class ItemList extends Activity {     public ListView listView;     public static List dataItems;     按钮PayPro;     静态NumberPicker选择器;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_list);
    listView = (ListView)findViewById(R.id.itemList);
    Custom_row.ProPay=(Button)findViewById(R.id.bProPay);
    Custom_row adapter = new Custom_row(this,
            R.layout.activity_custom_row, dataItems);
    listView.setAdapter(adapter);
}
}

`

2 个答案:

答案 0 :(得分:0)

您需要每次都设置侦听器,这样就不会在多行上重复使用相同的侦听器。

同样newValCount似乎与所有号码选择器共享 - 所以如果它改变了一个,它将全部改变。

public View getView(int position, View convertView, ViewGroup parent) {   
    ViewHolder holder = null;
    DataItem dataItem = getItem(position);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.activity_custom_row, null);
        holder = new ViewHolder();
        holder.Items = (TextView) convertView.findViewById(R.id.tvItem);
        holder.Cost = (TextView) convertView.findViewById(R.id.tvCost);
        holder.number =(NumberPicker)convertView.findViewById(R.id.numberPicker);
        holder.number.setMaxValue(10);
        holder.number.setMinValue(0);    
        holder.number.setFocusable(false);
        ItemList.picker = holder.number;

        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.Items.setText(dataItem.getItem());
    holder.Cost.setText(dataItem.getCost());
    holder.number.setValue(0);
    holder.number.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

                if(newVal!=0){
                    newValcount++; // THIS IS SHARED AMOUNGST ALL NUMBER PICKERS
                    if(newValcount > 0)
                        ProPay.setVisibility(View.VISIBLE);
                }
                else{
                    newValcount--;
                    if(newValcount==0)
                        ProPay.setVisibility(View.INVISIBLE);

                }
            }
        });
    return convertView;
}

if (convertView == null)内,您应该只创建对视图的引用。不添加值更改侦听器。 (这需要每次都对每件物品进行,而不是回收。

另外,不要在LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); getView内执行此操作static Button ProPay;每次都能获得性能不佳

同样Button会导致内存泄漏,因为ViewViewContextContextActivity是将Activity设为静态意味着即使在Activity不再显示在屏幕上,即内存泄漏

之后,您也会保留对apiVersion: extensions/v1beta1 kind: Ingress metadata: name: kube-ui spec: backend: serviceName: kube-ui servicePort: 80 的引用

答案 1 :(得分:0)

character

 data_from_many_cells[,as.character(special_cells[,1])]