2015-02-10 61 views
1

我有一個由數據庫填充的自定義列表適配器。當他們被點擊時,我需要從每行中的每個TextViews獲取文本。我如何告訴程序給我主容器的孩子?在android中訪問容器的子項

XML的容器:

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:layout_width="1dp" 
     android:layout_height="match_parent"> 
     <View style="@style/Divider" /> 
    </LinearLayout> 

    <TextView 
     android:layout_width="0dp" 
     android:gravity="center" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Year" 
     android:id="@+id/textView_year_tag" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:padding="10dip" 
     android:layout_weight="1"/> 

    <LinearLayout 
     android:layout_width="1dp" 
     android:layout_height="match_parent"> 
     <View style="@style/Divider" /> 
    </LinearLayout> 

    <TextView 
     android:layout_width="0dp" 
     android:gravity="center" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Mint" 
     android:id="@+id/textView_mint_tag" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:padding="10dip" 
     android:layout_weight="1"/> 

    <LinearLayout 
     android:layout_width="1dp" 
     android:layout_height="match_parent"> 
     <View style="@style/Divider" /> 
    </LinearLayout> 
    <TextView 
     android:layout_width="0dp" 
     android:gravity="center" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Specialty" 
     android:id="@+id/textView_specialty_tag" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:padding="10dip" 
     android:layout_weight="2"/> 

    <LinearLayout 
     android:layout_width="1dp" 
     android:layout_height="match_parent"> 
     <View style="@style/Divider" /> 
    </LinearLayout> 
    <TextView 
     android:layout_width="0dp" 
     android:gravity="center" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Mintage" 
     android:id="@+id/textView_mintage_tag" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:padding="10dip" 
     android:layout_weight="2"/> 

    <LinearLayout 
     android:layout_width="1dp" 
     android:layout_height="match_parent"> 
     <View style="@style/Divider" /> 
    </LinearLayout> 
</TableRow> 

類中,我處理了點擊:

public class PennyFlyingEagle extends ListActivity implements AdapterView.OnItemClickListener { 

    private CoinsDataSource datasource = new CoinsDataSource(this); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_blank); 
     addValues(); 
     setUpComponents(); 
    } 

    private void setUpComponents(){ 
     ArrayList<Coin> myValuesToDisplay = getDatabaseContent(); 
     MyCustomListAdapter adapter = new MyCustomListAdapter(this, myValuesToDisplay); 
     setListAdapter(adapter); 
     getListView().setOnItemClickListener(this); 
    } 

    private ArrayList<Coin> getDatabaseContent(){ 

     datasource.open(); 
     ArrayList<Coin> coins_list = datasource.getAllCoins(MySQLiteHelper.TABLE_PENNY); 
     return coins_list; 

    } 

    public void addValues() 
    { 
     PopulateDb makeDB = new PopulateDb(this); 
     makeDB.openDB(); 
     makeDB.setDBPenny(); 
     makeDB.closeDB(); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    //Get the text from the TextViews 
    } 
} 

回答

2
  • 選擇1:你可以獲取當前點擊的項目視圖從view
    參數onItemClick方法。然後通過findViewById您 可以獲得文本視圖和文本。
  • 選項2:您可以爲自定義適配器中的項目設置標籤。在
    getView中,可以將標記設置爲返回值view。之後, 您可以直接通過撥打 android.view.View.getTag()獲取文本(例如「2年」)參數onItemClick 方法。
2

把爵士,@Shao付諸實踐

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
//Get the text from the TextViews 
String text = ((TextView)view.findViewById(R.id.textviewtoget)).getText().toString(); 
} 

隨時接受邵爵士的答案..

+0

謝謝你把他的@Better邵的想法轉換成代碼,它幫助我瞭解多少更好 – Dommol 2015-02-11 02:07:54