Narendra Pal
Narendra Pal

Reputation: 6604

how to take the value form edittext in a listview?

I have a custom list with one textView and one edit text in each row. My question is: whenever user enters any number inside the edittext,at that time i want to take the value from the edittext and add it with the previous value and displayed in the top textview. For eg. Say inside number of edittext i entered 10 in any edittext. 10is the first number entered.then it is going to add with 0.after that if he enter 15 in another edittext,then 10+15 = 25 should be displayed in the top textview.

Upvotes: 1

Views: 570

Answers (5)

Archie.bpgc
Archie.bpgc

Reputation: 24012

In the Adapter, do this:

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

    View vi = convertView;
    ViewHolder holder = new ViewHolder();

    p = values.get(position);
    if (vi == null) {

            vi = inflater.inflate(R.layout.feed_items, null);
            holder.text = (TextView) vi.findViewById(R.id.tv);
            holder.editText = (EditText) vi.findViewById(R.id.et);

            holder.editText.addTextChangedListener(new TextWatcher(){
                            public void afterTextChanged(Editable s) {
                                i++;
                                int n = Integer.parseInt(holder.text.getText().toString());
                                int m = Integer.parseInt(s.getText().toString());
                                holder.text.setText("" + n + m);
                                }
                                public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                                public void onTextChanged(CharSequence s, int start, int before, int count){}
                    }); 

            vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();
    }
    return vi;
    }

Upvotes: 0

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

I am not sure,this would help...but you can try with this:

Get a setOnFocusListener on your edit text like:

mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View arg0, boolean arg1) {
         int s=Integer.parseInt(mEditText.getText().toString());
         int ps=Integer.parseInt(mTextView.getText().toString());
         mTextView.setText((s+ps)+"");
         mEditText.setText("");// clear editText after adding its value to textview
    }
});

don't forget to empty the edittext when focus is gone,otherwise when user would click onto edittext even to delete previous one,value would again be added to your textview.

Upvotes: 1

Narendra Pal
Narendra Pal

Reputation: 6604

I got it...

That was as simple as using setOnFocusListener. If the focus gets lost from the edit text.at that the boolean hasFocus parameter gets false. and we can easily collect the value inside the edit text. But thanks for you support guys. Thanks 1ce again.

Upvotes: 2

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

You have to Use A TextWatcher on the EditText View like this for That:

EditText editText1 = (EditText) findViewById(R.id.e1);

TextWatcher checker = new TextWatcher() { 
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (ChecknotNull()) {
            TextView1.setText(edittext1.getText().toString().trim());;
   Note: You can Also Go on Concating the values with the  
         previous values Of the TextView(InShort,Perform Logic Here)
       }
    }

    private boolean ChecknotNull() {
        return editText1.getText().toString().trim().length() > 0;
    }
};  

//Set the checker method for the EditText View like this Way
editText1.addTextChangedListener(checker);

Upvotes: 1

Vishwanath.M
Vishwanath.M

Reputation: 6317

try this

  String content = edtEditText.getText().toString();
  tvTextView.setText(content); 

Upvotes: 1

Related Questions