1

Still a total noob here...

I'm trying to render the specs for a football match (teams, time, tournament etc.)

I have created a team.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/teamImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="2dp"
    android:adjustViewBounds="true"
    android:src="@drawable/sofabold_launcher"
    android:layout_alignWithParentIfMissing="false"
    android:clickable="false"
    android:cropToPadding="true"
    android:contentDescription="@string/teamLogoContentDescription"/>

<TextView
    android:id="@id/teamName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/teamName"
    android:textSize="14sp"
    android:textStyle="bold"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/teamImageView"/>

</RelativeLayout>

A teams.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >

<include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/team"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/homeTeam"/>

<include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/team"
        android:layout_below="@id/homeTeam"
        android:id="@+id/awayTeam"/>
</RelativeLayout>

And then I have a matchLayout.xml including my teams.xml

My initial attempt was to just create one big layout with different element and reference them all by their id in my matchHolder and populating them using something like

matchHolder.homeTeamName.setText(match.getHomeTeamName());

Now I've tried changing my populateMatchHolder to:

private void populateMatchHolder(View convertView, BaseMatchHolder matchHolder) {
    matchHolder.teams = (RelativeLayout) convertView.findViewById(R.id.teams);
}

and now I cant figure out how to reference the TextView (home team name) inside the RelativeLayout (teams) inside another RelativeLayout (match).

Am I totally missing the point here? Do I need to have an id called homeTeamName somewhere? And awayTeamName? Isn't it just enough to have teamName twice? Once in the homeTeam include of team.xml and once in the awayTeam include of team.xml.

Hope it's somewhat clear what I mean :-) It's pretty late and I'm tired :-/

Thanks in advance

4

2 回答 2

1

If you want to reference view by id that id should be unique in the given scope, otherwise android will return only first view with given id.

So if you do something like this teamsView.findViewById(R.id.teamName) you only get the teamName view for homeTeam layout because there are two view with the same id on the teams.xml(on included for homeTeam and one included for awayTeam).

Solution: first get team layout for each team and then set the team name For example:

View homeTeam = teamsView.findViewById(R.id.homeTeam);
TextView homeTeamName = homeTeam.findViewById(R.id.teamName);
homeTeamName.setText("blablabal");
//the same for other team

BTW. use android:id="@+id/teamName" instead of android:id="@id/teamName"

于 2013-08-12T20:08:42.630 回答
1

You can refer it using the id of that textView. eg

<TextView
    android:id="@+id/teamName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/teamName"
    android:textSize="14sp"
    android:textStyle="bold"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/teamImageView"/>

You can refer it by id teamName. As you are using include tag in another layout, it becomes part of that parent layout.

One more thing use use android:id="@+id/teamName" instead of android:id="@id/teamName" cause whenever you use that particular id first time in the layout , it must be used with @+id, later you can simply use @id anywhere in the layout to refer that particular view.

于 2013-08-12T20:09:45.683 回答