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