2010-11-05 44 views
0

我正在開發一個小型應用程序,並使用填充了複合組件的列表視圖。這個組件有兩個文本視圖和一個按鈕。其中一個文本視圖是不可見的,當點擊該按鈕時,它應該出現。我可以顯示列表,但是當按鈕被點擊時,我不能使textview可見。 這裏是組件的XML:使用按鈕單擊來擴展listview自定義組件

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<TextView 
    android:text="@+id/TextView01" 
    android:id="@+id/placeNamwView" 
    android:layout_height="wrap_content" 
    android:textSize="30dp" 
    android:layout_width="wrap_content" 
    android:maxWidth="250dp"> 
    </TextView> 
<Button 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/placeNamwView" 
    android:layout_width="wrap_content" 
    android:id="@+id/Button01" 
    android:text="More"> 
    </Button> 
<EditText 
    android:layout_below="@+id/placeNamwView" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@+id/TextView01" 
    android:id="@+id/placeAddressView" 
    android:textSize="20dp" 
    android:textColor="#FFFF0000" 
    android:maxWidth="250dp"> 
    </EditText> 
</RelativeLayout> 

這是填充列表一個ArrayAdapter:

public class AddressAdapter extends ArrayAdapter<Item> { 

    int resource; 
    RelativeLayout placeView; 
    EditText addressText; 



    public AddressAdapter(Context _context, int _resource, List<Item> _items) { 
     super(_context, _resource, _items); 
     resource = _resource; 
    } 



    private OnClickListener buttonClick = new OnClickListener() { 
    public void onClick (View v) { 
     int i = placeView.findViewById(R.id.stub_import).getVisibility(); 
     visibility(i); 
    } 
    } 


    private void visibility(int i) { 
    // 

    TODO Auto-generated method stub 
    switch(i) { 
     case(View.GONE): 
     addressText.setVisibility(View.VISIBLE); 
     break; 
     case(View.VISIBLE): 
     addressText.setVisibility(View.GONE); 
     break; 
    } 



    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    Item item = getItem(position); 

    String name = item.getName(); 
    String address = item.getAddress(); 

    if (convertView == null) { 
     placeView = new RelativeLayout(getContext()); 
     String inflater = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater); 
     vi.inflate(resource, placeView, true); 
    } 
    else 
    { 
     placeView = (RelativeLayout) convertView; 
    } 

    TextView nameText = (TextView)placeView.findViewById(R.id.placeNamwView); 
    Button button = (Button)placeView.findViewById(R.id.Button01); 
    addressText = (EditText)placeView.findViewById(R.id.placeAddressText); 

    button.setOnClickListener(buttonClick); 
    nameText.setText(name); 
    addressText.setText(address); 

    return placeView; 
    } 
} 

回答

0

您正在使用的每一行相同placeView數據成員。因此,按鈕點擊將改變getView()處理的最後一行中的TextView可見性,而不是包含用戶點擊的按鈕的行。

Here is a sample project來自我的一本書,其中展示了ListView(本例中爲RatingBar)中交互式小部件的使用。在我的情況下,我用getTag()setTag()。在你的情況下,你可以撥打開始導航找到正確的TextView

0

我認爲你應該使用下面的條件

INVISIBLE 

,而不是

GONE 

試試吧!