Android如何使用按钮获取文本

时间:2015-06-09 08:18:27

标签: android json listview button

这是我的listview,当我点击确切位置时,我将参数作为字符串。

我想按照以下步骤添加按钮:

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

我在XML中添加了按钮,如下所示:

      <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" />

这是我无法按照我点击的位置获取字符串的方法:

    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 个答案:

答案 0 :(得分:1)

在这里,您无法获取值,因为您不知道按钮的视图父视图。 所以请使用以下方法查看按钮:

MPI_Rank