通过<ListView>将List<Object>里的属性显示出来

1:GameListActivity.java

public class GameListActivity extends Activity {

    private static List<Game> gamelist;
    private Handler h;
    private ListView l;

    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_list);
        l = (ListView) findViewById(R.id.listview);
//通过此线程从服务器获取list<Game>
        GameListThread gt = new GameListThread();
        Thread t = new Thread(gt);
        t.start();

        h = new Handler() {
            public void handleMessage(Message msg)  {  
                if(msg.what == 1) {
    //将List<Game>存于List<HashMap<String, Object>>中
                    List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
                    for(int i=0; i<gamelist.size(); i++) {
                        Game g = gamelist.get(i);
                        HashMap<String, Object> item = new HashMap<String, Object>();
                        item.put("id", g.getId());
                        item.put("gamelocation", g.getGamelocation());
                        item.put("gamename", g.getGamename());
                        item.put("gametime", g.getGametime());
                        item.put("personnum", g.getPersonnum());
                        data.add(item);
                    }
//第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
// 第二个参数表示生成一个Map(String ,Object)列表选项
// 第三个参数表示界面布局的id  表示该文件作为列表项的组件
// 第四个参数表示该Map对象的哪些key对应value来生成列表项
// 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
                    SimpleAdapter adapte = new SimpleAdapter(GameListActivity.this,data,R.layout.gameitem,
                            new String[] {"id","gamename","gamelocation","gametime","personnum"},
                            new int[] {R.id.id,R.id.gamename,R.id.gamelocation,R.id.gametime,R.id.personnum,}
                    );
                    l.setAdapter(adapte);
    //其中一条Item被点击是触发该事件
                    l.setOnItemClickListener(new OnItemClickListener() {
    //参数:参数1当前被点的条目所在的LIstView,参数2是当前条目的view对象,
    //参数3是当前点击的条目所绑定的数据在集合中的索引值,参数4是view界面在List中的排列的Id
                        public void onItemClick(AdapterView<?> arg0, View view,
                                int position, long id) {
    //通过intent从本页面向GameInforActivity中传递一个Game对象
                            Intent i = new Intent();
                            i.setClass(GameListActivity.this, GameInforActivity.class);
                            Bundle b = new Bundle();
    //game对象需要实现Serializable接口
                            b.putSerializable("gameinfor", gamelist.get(position));
                            i.putExtras(b);
                            startActivity(i);
                            finish();
                            }
                    });
                }
                else {
                    Toast.makeText(getApplicationContext(), "暂无比赛信息", Toast.LENGTH_SHORT).show();
                }
            }  
        };


    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.game_list, menu);
        return true;
    }

    class GameListThread implements Runnable {
        public void run() {
            gamelist = GameListSer.getGameList();
            Message m = new Message();
            if(gamelist != null) {
                m.what = 1;
                h.sendMessage(m);
                return;
            }
            else {
                m.what = -1;
                h.sendMessage(m);
                return;
            }
        }
    }

}

2:对应的列表项代码

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="fill_parent"
        android:layout_height="30dp"
         android:orientation="horizontal"
        >
    <TextView
    android:id="@+id/id"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="wrap_content"
     android:padding="20px"
     android:gravity="center"
         />
    <TextView
        android:gravity="center"
    android:id="@+id/gamename"
    android:layout_weight="2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:padding="20px"
         />
    <TextView
        android:gravity="center"
    android:id="@+id/gamelocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
     android:padding="20px"
         />
    <TextView
        android:gravity="center"
    android:id="@+id/gametime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
     android:padding="20px"
         />
    <TextView
        android:gravity="center"
    android:id="@+id/personnum"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="wrap_content"
     android:padding="20px"
         />
    </LinearLayout>

3;显示界面代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".GameListActivity" >
    <LinearLayout
       android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:orientation="horizontal"
          android:textSize="20sp"
        >
    <TextView
        android:textSize="20sp"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
    android:text="ID"
         />

     <TextView
    android:text="比赛名称"
    android:layout_weight="2"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

     <TextView
    android:text="比赛地点"
    android:layout_weight="2"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

     <TextView
    android:text="比赛时间"
    android:layout_weight="2"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <TextView
    android:text="当前人数"
    android:layout_weight="1"
   android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
     <TextView
    android:text="更多操作"
    android:layout_weight="2"
   android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    </LinearLayout>
    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

4:运行结果
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值