2013-12-10 89 views
-1

世界,你好獲取項目名稱

我又需要你的幫助,我有onclick事件的列表視圖,但我需要從被點擊項目獲得項目名稱。

這是我此刻...

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

       final int selected_row = position; 
       final Product user = (Product) parent.getItemAtPosition(position); 

       AlertDialog.Builder alert = new AlertDialog.Builder(nuevo_pedido.this); 

       alert.setTitle("Cantidad: "); 

       final NumberPicker np = new NumberPicker(nuevo_pedido.this); 
       String[] nums = new String[11]; 
       for(int i=0; i<nums.length; i++) 
         nums[i] = Integer.toString(i); 

       np.setMinValue(0); 
       np.setMaxValue(nums.length-1); 
       np.setWrapSelectorWheel(false); 
       np.setDisplayedValues(nums); 
       np.setValue(0); 


       alert.setView(np); 

       alert.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        // Do something with value! 

        int valor = np.getValue(); 

        Toast.makeText(getApplicationContext(), "Valor: " + valor + " Producto: " + user + " Usr: " + extraData, Toast.LENGTH_SHORT).show(); 

        } 
       }); 

       alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 

        } 
       }); 

       alert.show(); 
    } 

}); 

當我的代碼運行它的一切OK,但送東西究竟是不是名字。

[email protected]

建議?

回答

3

嘗試使用lv.getItemAtPosition(position)來獲取文本。

lv.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> arg0, View v, int position, 
    long arg3) 
{ 
    String name = arg0.getItemAtPosition(position).toString() 
} 
}); 

看看這個http://developer.android.com/reference/android/widget/AdapterView.html#getItemAtPosition%28int%29

+0

他已經在使用它。問題是他試圖將他的「產品」對象顯示爲字符串 – dymmeh

+0

該代碼發送相同的結果... [email protected] – bigmerol

0

從對象產品,將有可能通過一個getter抓不住的name屬性: product.getName()

或者問題是另一回事?

0
Product_listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 

我們可以從適配器視圖獲取對象

final Product user = (Product)parent.getAdapter().getItem(position); 

現在,你得到的吸氣劑產品的

名。

它爲我工作。