简体   繁体   中英

Getting value of EditText in Activity from ListView

My LisiViewAdapter Class is this

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

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. But I am not getting data from all the EditText I am getting only four values of 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. 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.

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 . Other element are not still inflated on the screen. for that you need to first UNDERSTAND THE WORKING OF LISTVIEW WITH ADAPTER.

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 . 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.

PLEASE UNDERSTAND THE BASIC OF LISTVIEW WITH ADAPTER YOU WILL UNDERSTAND THE WORKING OF OTHER ADAPTERS.

hope you understand.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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