Android how am I supposed to get text using button

105 Views Asked by At

This is my listview where I am getting my parameters as string when I am clicking at the exact position.

I want to do it same by adding the button as follows:

 listViewCompanyTxnCartSelect.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                        subtypeIDuniqueCompanyTxnCartSelectListView = ((TextView) view.findViewById(R.id.textViewSubTypeIdUniqueListView)).getText().toString();
                        companyEventIDUniqueCompanyTxnCartSelectListView = ((TextView) view.findViewById(R.id.textViewcompanyEventIDUniqueListView)).getText().toString();
                        seatsCompanyTxnCartSelectListView = ((TextView) view.findViewById(R.id.textViewTxnCartSelectSeats)).getText().toString();

                        System.out.println("subtypeIDuniqueCompanyTxnCartSelect"+subtypeIDuniqueCompanyTxnCartSelectListView);
                        System.out.println("companyEventIDUniqueCompanyTxnCartSelectListView"+companyEventIDUniqueCompanyTxnCartSelectListView);
                        System.out.println("seatsCompanyTxnCartSelect"+seatsCompanyTxnCartSelectListView);

                        companyTxnCartDelete(paramCompanyTxnCartDelete);

                        companyTxnCartSelect(paramCompanyTxnCartSelect);
                    }
                });

I have added the button in my XML as follows:

      <ImageButton
            android:id="@+id/imageButtonDelete"
            android:layout_width="@dimen/width_button"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/textViewTxnCartSelectStandDesc"
            android:contentDescription="@string/app_name"
            android:onClick="removeOnClickHandler"
            android:src="@android:drawable/ic_menu_delete" />

Here is the method where I can't get my strings as per the position where I am clicking:

    public void removeAtomPayOnClickHandler(View view) {

            int position = listViewCompanyTxnCartSelect.getPositionForView(view);

            TextView tv = (TextView) findViewById(R.id.textViewSubTypeIdUniqueListView);

            String get = tv.getText().toString();

            System.out.println("get"+get);

            Toast.makeText(getApplicationContext(), "Hello there"+position+" "+get, Toast.LENGTH_SHORT).show();

        }
1

There are 1 best solutions below

0
On

Here you cannot get the values because you do not know the view parent view of the button. So please get the view of the button using this :

public void removeAtomPayOnClickHandler(View view) {

         int position = listViewCompanyTxnCartSelect.getPositionForView(view);
         View view=listViewCompanyTxnCartSelect.getChildAt(position);

         TextView tv = (TextView)view.findViewById(R.id.textViewSubTypeIdUniqueListView);

         String get = tv.getText().toString();

         System.out.println("get"+get);

         Toast.makeText(getApplicationContext(), "Hello there"+position+" "+get, Toast.LENGTH_SHORT).show();

    }