2012-10-25 49 views
-1

我有一個自定義表格單元格,基本上有2個按鈕,一個textview和一個編輯文本。在每個編輯文本旁邊都有一個加號和減號按鈕。有效地增加/減少文本框中的數字。Android:Listview自定義單元格 - 知道哪個項目

我無法弄清楚如何將按鈕鏈接到正確的文本框。即當我按加號時,有一個例程只適用於行中的edittext框。我是否必須爲每個文本框設置自定義ID?

我有所有的問題正確出來的文字。

我的自定義單元格是:

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

我的自定義單元格被稱爲有:

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


} 

我也必須從整理所有的信息之後的所有文本框,併爲每個文本寫一個查詢。我有一個提交按鈕,爲每個提供一個? (是否有一個自動創建數組或類似?)

湯姆

+1

提供您的整個XML文件,因爲您已將android:layout_weight分配給前兩個組件,而不是最後一個。 –

+0

你有沒有試過爲這兩個按鈕實現一個監聽器? – Ankush

+0

如果你爲不同的行元素賦予相同的id而不是改變它並且使它變得獨一無二 – Aamirkhan

回答

0

這是你需要什麼?

在onclick()按鈕的使用

@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; 
+0

非常感謝此,它非常有幫助。我現在無法解決的事情是如何將位置3處的按鈕按鈕與位置3處的文本框等鏈接起來。 – TMB87

+0

現在,在特定位置更改mDescription列表項的值。 (我的意思是刪除該位置的項目,並在相同位置插入新值並刷新適配器) –

1

你只需要添加監聽器,這裏是你如何可以在每個項目指定監聽器按鈕的例子:

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

    btPlus.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 


    } 
    }); 

    return v; 
}