android:在xml中显示数据

时间:2012-01-31 09:02:13

标签: android android-layout android-xml

我想创建一个游戏列表,其中会显示对手名称,分数等游戏详情。我在将数据提取到xml然后显示它时遇到问题。请帮助。

我创建了两个xml。我必须将数据设置为第二个xml,然后将第二个xml包含在第一个xml中。 我的第一个xml是

<FrameLayout
        android:id="@+id/gameListFrame"
        android:layout_width="fill_parent"
        android:layout_height="380dp"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dip" >

            <LinearLayout
                android:id="@+id/GameListHolder"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/scrollBackG"
                android:orientation="horizontal" >

           </LinearLayout>
        </ScrollView>
    </FrameLayout>

第二个xml是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gamelist_items"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="50px"
        android:gravity="left"
        android:orientation="horizontal"
        android:background="#FFF" >

        <ImageView
            android:id="@+id/gl_avatar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <LinearLayout android:orientation="horizontal"
            android:layout_width="225px"
            android:layout_height="fill_parent"
            android:gravity="left" 
            android:background="#000">
            <LinearLayout android:orientation="vertical"
                android:layout_width="175px"
                android:layout_height="wrap_content"
                android:gravity="top">
                <TextView android:id="@+id/oppName"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
                <LinearLayout android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="top" >
                    <TextView android:id="@+id/lastWord"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        />
                     <TextView android:id="@+id/diffTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        />
                  </LinearLayout>
             </LinearLayout>


        </LinearLayout>
    </LinearLayout>

</LinearLayout>

我的代码是

gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);

for (i=0;i<gamelistVector.size();i++)
{


 rowView=View.inflate(context, R.layout.gamelist_details,null );
TextView oppName=(TextView) rowView.findViewById(R.id.oppName); 
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
oppName.setText(gList.getOppName());
lastWord.setText(gList.getOppLastWord());
diffTime.setText(gList.getDiffTime()+"hr");
gameListItem.addView(rowView);  //show the total games}

3 个答案:

答案 0 :(得分:0)

您需要扩展ListAdapter并覆盖设置每行数据的getView()方法。

请参阅this example

答案 1 :(得分:0)

您需要inflater参考来夸大视图。

GetInflater参考使用:

LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);

gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);

    for (i=0;i<gamelistVector.size();i++)
    {
        rowView=inflater.inflate(R.layout.gamelist_details, null);             
        TextView oppName=(TextView) rowView.findViewById(R.id.oppName); 
        TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);           
        TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
        oppName.setText(gList.getOppName());                            
        lastWord.setText(gList.getOppLastWord());                            
        diffTime.setText(gList.getDiffTime()+"hr");                     
        gameListItem.addView(rowView);  //show the total games
    }

答案 2 :(得分:0)

谢谢你们4回答我的询问......我看到了你们的建议并想出了办法。这是我做过的工作......

private void doDisplay(){
    this.runOnUiThread(new Thread() {
        @Override
        public void run() {
            TextView gameCount=(TextView) findViewById(R.id.game_count);
            gameCount.setText(" found "+maxGames+" games ");

            for (i=0;i<gamelistVector.size();i++)
            {

                rowView=View.inflate(context, R.layout.gamelist_details, null);
                TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
                TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
                TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
                TextView score=(TextView) rowView.findViewById(R.id.opplastMoveScore);
                oppName.setText(" "+gamelistVector.get(i).getOppName());
                lastWord.setText(" "+gamelistVector.get(i).getOppLastWord());
                diffTime.setText(gamelistVector.get(i).getDiffTime()+"");
                score.setText("("+gamelistVector.get(i).getLastMoveScore()+")");

                ImageView oppImage = (ImageView) rowView.findViewById(R.id.gl_avatar);
                oppImage.setImageBitmap(gamelistVector.get(i).getOppImage());
                ImageView status = (ImageView) rowView.findViewById(R.id.gl_status);
                status.setImageResource(gamelistVector.get(i).getTurnUserID());


                gameListItem.addView(rowView);

            }
        }
    });    
}