简体   繁体   English

从ListView获取Activity中EditText的值

[英]Getting value of EditText in Activity from ListView

My LisiViewAdapter Class is this 我的LisiViewAdapter类是这个

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;
    }
}

In my Activity class I am getting the value of EditText 在我的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());
        } 

Size of list is five. 清单的大小是5。 But I am not getting data from all the EditText I am getting only four values of edit Text. 但是我没有从所有EditText中获取数据,我只得到了Edit Text的四个值。 Not getting last value. 没有得到最后的价值。 Please Help. 请帮忙。

I think you should not get data like this. 我认为您不应获取此类数据。 Make a data List into adapter to store data. data List放入适配器以存储数据。 You should know that when you scroll that listview, the upper view will be reused, so all your data will be lost. 您应该知道,当滚动该列表视图时,上部视图将被重用,因此所有数据都将丢失。 You may only have five item, but use data list to store the data is a good idea using get data from the dapter. 您可能只有五个项目,但是使用数据列表存储数据是一个很好的主意,可以使用从适配器中get data

You will get only four element on our for loop because only four element may be shown on your mobile screen , or mobile screen have the capacity of showing only four element . 在我们的for循环中,您只会得到四个元素,因为您的移动屏幕上可能只会显示四个元素,或者移动屏幕只能显示四个元素。 Other element are not still inflated on the screen. 屏幕上其他元素仍未膨胀。 for that you need to first UNDERSTAND THE WORKING OF LISTVIEW WITH ADAPTER. 为此,您需要首先了解如何使用ADAPTER进行LISTVIEW的工作。

If you are using listView with adapter the screen shows only four ( or as pre the capacity of the screen ) items , because listView does not make places for all the element that you pass to the adapter . 如果您将listView与适配器一起使用,则屏幕仅显示四个(或作为屏幕容量的前一项),因为listView不会为传递给适配器的所有元素放置位置。 It makes place for element that are shown on the screen. 它为屏幕上显示的元素留出空间。

When you scroll up or down the places are made for next elements and previous element stored in the RECYCLER. 上下滚动时,将为下一个元素和上一个元素存储在RECYCLER中。

PLEASE UNDERSTAND THE BASIC OF LISTVIEW WITH ADAPTER YOU WILL UNDERSTAND THE WORKING OF OTHER ADAPTERS. 请通过适配器了解Listview的基本知识,您将了解其他适配器的工作原理。

hope you understand. 希望你能理解。

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

use this link to understand 使用此链接了解

Finally got the Solution! 终于得到了解决方案! This is my Adapter Class 这是我的适配器类

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;
    }
}

In Activity Class 活动课

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 从这里得到解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM