有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓中访问容器的子级

我有一个由数据库填充的自定义列表适配器。我需要在单击每行中的TextViews时从中获取文本。我如何告诉程序给我主容器的子容器

XML对于容器:

<TableRow
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent">

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

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

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

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

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

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

    <LinearLayout
        安卓:layout_width="1dp"
        安卓: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. # 1 楼答案

    • 选项1:您可以从view
      onItemClick方法的参数。然后通过你 可以获取文本视图和文本
    • 选项2:您可以为自定义适配器中的项目设置标记。在
      getView,您可以将标签设置为返回值view。之后, 您只需拨打电话直接获取文本(如“2年”) ^在onItemClickview参数上{} 方法
  2. # 2 楼答案

    将邵先生付诸实践

    @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(); 
    }
    

    欢迎接受邵先生的回答