-1

I have a custom table cell which basically has 2 buttons, one textview and one edit text. Next to each edit text there is a plus and minus button. Effectively to increment / decrement the number in the text box.

I can't work out how I link the buttons to the right text box. I.e. when I press plus, have a routine that only works for the edittext box in it's row. Do I have to set a custom ID for each text box?

I have all the text coming out correctly for the questions.

My custom cell is:

<TextView
    android:id="@+id/txtOption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_weight="0.00"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/btnPlus"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="49dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtOption"
    android:layout_weight="0.00"
    android:text="+" />

<Button
    android:id="@+id/btnMinus"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="49dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnPlus"
    android:layout_alignBottom="@+id/btnPlus"
    android:layout_toRightOf="@+id/btnPlus"
    android:layout_weight="0.00"
    android:text="-" />

<EditText
    android:id="@+id/tbAnswer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnMinus"
    android:layout_toRightOf="@+id/btnMinus"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

My custom cell is being called with:

    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);

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

        TextView tv=(TextView)v.findViewById(R.id.txtOption);

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

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


        return v;
    }


}

I will also have to collate all the information from all of the textboxes afterwards and write one query for each. Do I have a submit button that does a for each? (Is there an auto array created or similar?)

Tom

MissCoder87
  • 2,669
  • 10
  • 47
  • 82

2 Answers2

1

You just need to add listener, here is the example how can you assign listener for the button in every item:

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

  btPlus.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {


     }
  });

  return v;
}
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Nirali
  • 13,571
  • 6
  • 40
  • 53
0

Is this what you need ?

In the onclick() of the button use

@Override
    public void onClick(View v) {

            try {

                position = listview
                        .getPositionForView((View) v.getParent()); // position is an integer variable which helps you identify which item's plus button  was clicked
                Log.v("Position id", "" + position);

                // do some manipulations 
                notifyDataSetChanged();

            } catch (Exception e) {

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

            }

            break;
G_S
  • 7,068
  • 2
  • 21
  • 51
  • Thanks a lot for this, It's very helpful. The thing I can't work out now is how to link say button press at position 3 with textbox at position 3 etc – MissCoder87 Oct 25 '12 at 12:23
  • Now change the value of mDescription's list item at the particular position. (I mean remove the item at that position and insert the new value at the same position and refresh the adapter) – G_S Oct 25 '12 at 12:37