2015-06-09 20 views
0

這是我的列表視圖,當我點擊確切位置時,我將字符串參數作爲字符串獲取。Android我該如何使用按鈕獲取文本

我想通過添加按鈕同樣做到這一點,如下所示:

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

這裏是方法,我可以」 t按照我點擊的位置獲取我的字符串:

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

     } 
+0

現在的結果是什麼? – MD1948

+0

你能解釋「我想通過添加按鈕來做同樣的事情嗎」 –

回答

1

在這裏,您無法獲取值,因爲您執行n不知道按鈕的視圖父視圖。 所以請使用此按鈕查看按鈕:

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

    } 
相關問題