简体   繁体   中英

Get values of edittext in listview

I want get values of edittext in listview. This is my CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

Context context;
List<RowItem> rowItem;

ImageView imgIcon, add_cart;
TextView txtTitle;

EditText colli, prezzo, quantita;
String colliStr, prezzoStr, quantitaStr;

ArrayList<String> ArrayPrezzo = new ArrayList<String>();
ArrayList<String> ArrayQuantita = new ArrayList<String>();
ArrayList<String> ArrayColli = new ArrayList<String>();

CustomAdapter(Context context, List<RowItem> rowItem) {
    this.context = context;
    this.rowItem = rowItem;

}

@Override
public int getCount() {

    return rowItem.size();
}

@Override
public Object getItem(int position) {

    return rowItem.get(position);
}

@Override
public long getItemId(int position) {

    return rowItem.indexOf(getItem(position));
}
@Override
public boolean  areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return false;
}

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

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.single_row, null);
    }

    imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    txtTitle = (TextView) convertView.findViewById(R.id.title);
    add_cart = (ImageView) convertView.findViewById(R.id.icon);

    colli = (EditText) convertView.findViewById(R.id.editText3);
    prezzo = (EditText) convertView.findViewById(R.id.editText2);
    quantita = (EditText) convertView.findViewById(R.id.editText);

    add_cart.setFocusable(true);
    add_cart.setClickable(true);

    final RowItem row_pos = rowItem.get(position);
    // setting the image resource and title
    imgIcon.setImageResource(R.drawable.product);
    txtTitle.setText(row_pos.getTitle());

    add_cart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // HERE I WANT GET THE VALUES OF EDITTEXT;

        }
    });

        return convertView;
}

can someone help me please? In ly listview there are: textview | edittext1 | edittext2 | edittext3 | imageview I don't know how to get values of edittext at position current when i click the button. Can you make a example with my code? Thnank you

UPDATE: I have edited the listener:

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

            prezzoStr = prezzo.getText().toString();
            Toast.makeText(context, prezzoStr, Toast.LENGTH_SHORT)
                    .show();

        }
    });

but prezzoStr is empty. Toast doesn't show nothing. I think need use row_pos variable for getting the value of a determinate edittext. Help please

It's not difficult

add_cart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          String colliValue = colli.getText().toString());
          String prezzoValue = prezzo.getText().toString());
          String quantitaValue = quantita.getText().toString());
        }
});

You have to get parent view and then find text view by id:

 add_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    TextView prezzo = (TextView) findByIdRecursively(v, R.id.editText2);
               String prezzoStr = prezzo.getText().toString();
            }
        });

...

public View findByIdRecursively(View view, int targetId) {
    View result = view.findViewById(targetId);
    if (result != null) {
        return result;
    }
    View parent = (View) view.getParent();
    if (parent == null) {
        return null;
    }
    return findByIdRecursively(parent, targetId);
}

The view in onClick is the view that was clicked (add_cart in this case)

... or you can do:

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

...

    add_cart = (ImageView) convertView.findViewById(R.id.icon);
    add_cart.setTag(convertView);

    add_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    TextView prezzo = (TextView) ((View)v.getTag()).findViewById(R.id.editText2);
               String prezzoStr = prezzo.getText().toString();
            }
        });

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