从ListView获取Activity中的EditText值

时间:2014-09-17 05:06:30

标签: java android listview baseadapter

My LisiViewAdapter Class就是这个

public class ListViewAdapter3 extends BaseAdapter {
Activity context;

String productCode[];
String productName[];
String productType[];
String productPrice[];
String lastFourOrder[];
String productId[];

public ListViewAdapter3(Activity context, String productCode[],
        String productName[], String productType[], String productPrice[],
        String lastFourOrder[], String productId[]) {
    super();
    this.context = context;
    this.productCode = productCode;
    this.productName = productName;
    this.productType = productType;
    this.productPrice = productPrice;
    this.lastFourOrder = lastFourOrder;
    this.productId = productId;
}

public int getCount() {
    // TODO Auto-generated method stub
    return productName.length;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private class ViewHolder {
    TextView tvproductname;
    TextView tvproducttype;
    TextView tvproductunit;
    TextView tvproductprice;
    TextView tvproductorder;
    ImageView ivup1;
    ImageView ivdown1;
    EditText stepper_display;
    int defaultNumber = 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;

    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.collection2, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();

    }

    holder.tvproductname = (TextView) convertView
            .findViewById(R.id.tvproduct);
    holder.tvproducttype = (TextView) convertView
            .findViewById(R.id.tvprodtype);
    holder.tvproductunit = (TextView) convertView
            .findViewById(R.id.tvprodunit);
    holder.tvproductprice = (TextView) convertView
            .findViewById(R.id.tvprice);
    holder.tvproductorder = (TextView) convertView.findViewById(R.id.tvL40);
    holder.ivup1 = (ImageView) convertView.findViewById(R.id.ivup1);
    holder.ivdown1 = (ImageView) convertView.findViewById(R.id.ivdown1);
    holder.stepper_display = (EditText) convertView
            .findViewById(R.id.etorderqty);
    holder.stepper_display.setText("" + holder.defaultNumber);

    holder.ivup1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s = holder.stepper_display.getText().toString();
            if (s.equals("0")) {
                holder.defaultNumber = 0;
                holder.defaultNumber++;
            } else {
                // decrem_btn.setClickable(true);
                holder.defaultNumber++;
            }
            holder.ivdown1.setClickable(true);

            String currentValue = Integer.toString(holder.defaultNumber);

            holder.stepper_display.setText(currentValue);

        }
    });

    holder.ivdown1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s = holder.stepper_display.getText().toString();
            // System.out.println("s=====" + s);
            if (s.equals("0")) {
                // System.out.println("ffffffffffffffffffffffffffff");
                holder.ivdown1.setClickable(false);
                holder.defaultNumber = 0;
            } else {
                // decrem_btn.setClickable(true);
                holder.defaultNumber--;
            }

            if (holder.defaultNumber == 0) {
                holder.ivdown1.setClickable(false);
            }
            String currentValue = Integer.toString(holder.defaultNumber);

            holder.stepper_display.setText(currentValue);
        }
    });

    holder.tvproductname.setText(productName[position]);
    holder.tvproducttype.setText(productType[position]);
    holder.tvproductunit.setText("");
    holder.tvproductprice.setText(productPrice[position]);
    holder.tvproductorder.setText(lastFourOrder[position]);
    return convertView;
    }
}

在我的Activity类中,我获得了EditText的值

for (int i = 0; i < list.getAdapter().getCount(); i++) {
        View view = list.getChildAt(i);
        EditText editText = (EditText) view.findViewById(R.id.etorderqty);
        System.out.println(i+" A:"+editText.getText().toString());
        } 

列表大小为五。但我没有从所有EditText获取数据我只获得了编辑文本的四个值。没有得到最后的价值。请帮忙。

3 个答案:

答案 0 :(得分:0)

我认为你不应该得到这样的数据。将data List变为适配器以存储数据。您应该知道,当您滚动列表视图时,上部视图将被重用,因此您的所有数据都将丢失。您可能只有五个项目,但使用数据列表存储数据最好使用来自dapter的get data

答案 1 :(得分:0)

我们的for循环只能获得四个元素,因为您的移动屏幕上只能显示四个元素,或者移动屏幕只能显示四个元素。其他元素仍未在屏幕上充气。为此你需要首先理解使用适配器进行LISTVIEW的工作。

如果您使用带适配器的listView,则屏幕仅显示四个(或者屏幕容量之前)项目,因为listView不会为您传递给适配器的所有元素创建位置。它为屏幕上显示的元素提供了位置。

向上或向下滚动时,会为下一个元素和存储在RECYCLER中的上一个元素创建位置。

请理解使用适配器的基本知识,您将理解其他适配器的工作。

希望你明白。

http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

使用此链接了解

答案 2 :(得分:0)

终于得到了解决方案!     这是我的适配器类

public class ListViewAdapter3 extends BaseAdapter {
Activity context;
String productCode[];
String productName[];
String productType[];
String productPrice[];
String lastFourOrder[];
String productId[]; 
// Created String Array Here 
private final String[] valueList;
public ListViewAdapter3(Activity context, String productCode[],
        String productName[], String productType[], String productPrice[],
        String lastFourOrder[], String productId[]) {
    super();
    this.context = context;
    this.productCode = productCode;
    this.productName = productName;
    this.productType = productType;
    this.productPrice = productPrice;
    this.lastFourOrder = lastFourOrder;
    this.productId = productId;
   //initialization of array
    valueList = new String[productCode.length];
}

public int getCount() {
    // TODO Auto-generated method stub
    return productName.length;
}



public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public class ViewHolder {
    TextView tvproductname;
    TextView tvproducttype;
    TextView tvproductunit;
    TextView tvproductprice;
    TextView tvproductorder;
    TextView tvproductid;
    ImageView ivup1;
    ImageView ivdown1;
    EditText stepper_display;       
    int defaultNumber = 0;      
}
 @Override
  public int getItemViewType(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    return 500;
}
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;

    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        //System.out.println("convertView Null ="+convertView);
        convertView = inflater.inflate(R.layout.collection2, null);
        holder = new ViewHolder();
        holder.tvproductname = (TextView) convertView.findViewById(R.id.tvproduct);
        holder.tvproducttype = (TextView) convertView.findViewById(R.id.tvprodtype);
        holder.tvproductunit = (TextView) convertView.findViewById(R.id.tvprodunit);
        holder.tvproductprice = (TextView) convertView.findViewById(R.id.tvprice);
        holder.tvproductorder = (TextView) convertView.findViewById(R.id.tvL40);
        holder.tvproductid = (TextView) convertView.findViewById(R.id.tvproductid);
        convertView.setTag(holder);

    } else {
        //System.out.println("convertView NOt Null ="+convertView);
        holder = (ViewHolder) convertView.getTag();

    }   


    holder.ivup1 = (ImageView) convertView.findViewById(R.id.ivup1);

    holder.ivdown1 = (ImageView) convertView.findViewById(R.id.ivdown1);

    holder.stepper_display = (EditText) convertView.findViewById(R.id.etorderqty);

    holder.stepper_display.setText("" + holder.defaultNumber);

    holder.ivup1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s = holder.stepper_display.getText().toString();
            if (s.equals("0")) {
                holder.defaultNumber = 0;
                holder.defaultNumber++;
            } else {
                // decrem_btn.setClickable(true);
                holder.defaultNumber++;
            }
            holder.ivdown1.setClickable(true);
            holder.stepper_display.setText(Integer.toString(holder.defaultNumber));

        }

    });

    holder.ivdown1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s = holder.stepper_display.getText().toString();
            // System.out.println("s=====" + s);
            if (s.equals("0")) {                    
                holder.ivdown1.setClickable(false);
                holder.defaultNumber = 0;
            } else {
                // decrem_btn.setClickable(true);
                holder.defaultNumber--;
            }

            if (holder.defaultNumber == 0) {
                holder.ivdown1.setClickable(false);
            }
            holder.stepper_display.setText(Integer.toString(holder.defaultNumber));

        }
    });

    holder.stepper_display.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
           //filled array here
           valueList[position] = s.toString();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    holder.tvproductname.setText(productName[position]);
    holder.tvproducttype.setText(productType[position]);
    holder.tvproductunit.setText("");
    holder.tvproductprice.setText(productPrice[position]);
    holder.tvproductorder.setText(lastFourOrder[position]);
    holder.tvproductid.setText(productId[position]);        
    return convertView;
}  

 // created method here 
 public String[] getValueList(){
        return valueList;
    }
}

在活动类

String[] string = lv.getValueList();
            for (int i = 0; i < list.getAdapter().getCount(); i++) {
                    View v1 = getViewByPosition(i, list);
                    String editTextValue = string[i];
      }

public View getViewByPosition(int position, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (position < firstListItemPosition || position > lastListItemPosition ) {
        return listView.getAdapter().getView(position, null, listView);
    } else {
        final int childIndex = position - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
  }

Got Solution from here