简体   繁体   English

在列表视图中获取edittext的值

[英]Get values of edittext in listview

I want get values of edittext in listview. 我想在列表视图中获取edittext的值。 This is my CustomAdapter.java 这是我的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 | 在ly listview中有:textview | edittext1 | edittext1 | edittext2 | edittext2 | edittext3 | edittext3 | imageview I don't know how to get values of edittext at position current when i click the button. imageview我不知道如何在单击按钮时获取当前位置的edittext值。 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. 但prezzoStr为空。 Toast doesn't show nothing. 吐司什么也没显示。 I think need use row_pos variable for getting the value of a determinate edittext. 我认为需要使用row_pos变量来获取确定的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: 您必须先获取父视图,然后按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) onClick中的视图是被单击的视图(在这种情况下为add_cart)

... 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();
            }
        });

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

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