0

I have a ListViewwhich has a custom cell in it with 2 buttons, a label and an editText. I've got it so that when I click the button for the custom cell it does a log saying at what position the button has been pressed. However I can't work out how I make it so when I press the button, it changes the text in the textbox for that cell. I can't work out how to reference it at a position.

My XML is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <ListView
         android:id="@+id/lvItems"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:drawSelectorOnTop="false" >

    </ListView>

</RelativeLayout>

My Code:

    class CustomAdapter extends BaseAdapter
{

@Override
public int getCount() {

    return mDescription.size();
}

@Override
public Object getItem(int arg0) {

    return null;
}

@Override
public long getItemId(int arg0) {

    return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

    LayoutInflater inf=getLayoutInflater();
    View v=inf.inflate(R.layout.noncriticalasset, arg2,false);
    TextView tv=(TextView)v.findViewById(R.id.txtOption);

    final EditText et =(EditText)v.findViewById(R.id.tbAnswer);

    Button btPlus=(Button)v.findViewById(R.id.btnPlus);
    Button btMinus=(Button)v.findViewById(R.id.btnMinus);

    btMinus.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {

               int position = listView
                        .getPositionForView((View) v.getParent());
                Log.v("Position id", "" + position);

                et.setText("Test Text");

                notifyDataSetChanged();

            } catch (Exception e) {

                Log.e("error", "error" + e.getMessage());

            }


        }
    });



    tv.setText(mDescription.get(arg0).toString()); 



    return v;
}

I tried referencing the textbox with just an et.setText() but it didn't work. Is there a way to say atPosisiton.et.setText etc?

Thanks a lot.

Ant4res
  • 1,217
  • 1
  • 18
  • 36
MissCoder87
  • 2,669
  • 10
  • 47
  • 82

4 Answers4

1

First of all I would insist you to use recycling pattern of ListView, which will work faster as it reduces the creation of the ListView row item views everytime when ListView scroll and re-uses the already created/instantiated view. And for maintaining the value of EditText/CheckBox you have to use setTag() for maintaining its position when ListView scrolls. I had written a blog post for the same here.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

hi declare Button in Adapter not in get view and ViewHolder. like below

Button btPlus,btMinus; ViewHolder holder ;

class ViewHolder {
  int fieldPossition;
 }

@Override public View getView(int position, View v, ViewGroup arg2) {

 holder = new ViewHolder();
LayoutInflater inf=getLayoutInflater();
  v=inf.inflate(R.layout.noncriticalasset, arg2,false);
    TextView tv=(TextView)v.findViewById(R.id.txtOption);

    EditText et =(EditText)v.findViewById(R.id.tbAnswer);

    btPlus=(Button)v.findViewById(R.id.btnPlus);
    btMinus=(Button)v.findViewById(R.id.btnMinus);


      fieldPossition = position;

    btMinus.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {

            ViewHolder deleteHolder = (ViewHolder) v.getTag();
                    //   
        int pos = deleteHolder.fieldpossition;
                Log.v("Position id", "" + position);

                et.setText("Test Text");

                notifyDataSetChanged();

            } catch (Exception e) {

                Log.e("error", "error" + e.getMessage());

            }


        }
    });



    tv.setText(mDescription.get(position).toString()); 

   btMinus.setTag(holder);


    return v;
}

thats it .

Ashish Wadatkar
  • 427
  • 2
  • 5
  • 19
0

Actually the position you got is right,but with notifyDataSetChanged() called, ListView will use getView() method to replace all item views with new ones. So,you should keep the clicked position somewhere ,and setup the EditText in getView().

fullahouse
  • 141
  • 2
-1

EDIT Tested OK
LazyViewHolder.class

 public class LazyViewHolder {
        private EditText et;
        private TextView text;
        private Button btnSub;
        public LazyViewHolder() {
            // TODO Auto-generated constructor stub
        }

        LazyViewHolder(TextView textView,EditText ed,Button btn) {
            super();
            this.setEt(ed);
            this.setText(textView);
            this.setBtnSub(btn);
        }

        public EditText getEt() {
            return et;
        }

        public void setEt(EditText et) {
            this.et = et;
        }

        public TextView getText() {
            return text;
        }

        public void setText(TextView text) {
            this.text = text;
        }

        public Button getBtnSub() {
            return btnSub;
        }

        public void setBtnSub(Button btnSub) {
            this.btnSub = btnSub;
        }




    }

getView()in Custom Adapter

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        final TextView textView;
        final EditText et;
        final Button sub;

        if (v == null) {
            v = inflater.inflate(R.layout.row, null);
            textView = (TextView) v.findViewById(R.id.tvLabel);
            textView.setText("Hello"+position);
            et = (EditText) v.findViewById(R.id.etWhatToFill);
            sub = (Button) v.findViewById(R.id.btnSubmit);
            v.setTag(new LazyViewHolder(textView, et, sub));
            sub.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                textView.setText(et.getText().toString());  
                }
            });
        } else {
            LazyViewHolder viewHolder = (LazyViewHolder) v.getTag();
            sub = viewHolder.getBtnSub();
            et=viewHolder.getEt();
            textView = viewHolder.getText();
        }
        return v;
    }

Please Find Detailed Explanation for ListView and CustomListView here

Just Variable
  • 892
  • 10
  • 19
  • I have made changes to the code what Tom has posted ,I guess we should give time for him to reply rather than wasting time here, if he needs explanation for code what he has made then I will be happy to give that :) – Just Variable Oct 26 '12 at 10:00
  • Thanks for this. It doesn't error, however it doesn't update the text either – MissCoder87 Oct 26 '12 at 10:25
  • I think you should need to Tag your views inside http://www.geekmind.net/2009/11/android-custom-list-item-with-nested.html please refer this by the time I will try to update the code – Just Variable Oct 26 '12 at 10:35
  • Hi Sorry for replying late I have tested my code in edit its working fine you can make changes according to your requirement, Please accept the answer if it helps you – Just Variable Oct 26 '12 at 12:16