ListView应用------ListView控件显示List集合中的数据,并对Item响应选择事件(法二)

来源:互联网 发布:淘宝提货方式电子券 编辑:程序博客网 时间:2024/05/22 15:24

本方法,采用继承ListActivity,来实现的,需要注意的是,在配置文件中ListView的ID书写方式@id/android:list

以及在对应的Activity中通过调用setListAdapter(adapter);实现数据填充;简化代码书写。


item.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   <LinearLayout android:orientation="horizontal"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:paddingLeft="10dp"  
  6.     android:paddingRight="10dp"  
  7.     android:paddingTop="1dp"  
  8.     android:paddingBottom="1dp"  
  9.     xmlns:android="http://schemas.android.com/apk/res/android" >  
  10.         <TextView   
  11.             android:id="@+id/scoreId"  
  12.             android:layout_width="60dip"  
  13.             android:layout_height="30dip"   
  14.             android:textSize="10pt"  
  15.             android:singleLine="true"/>  
  16.         <TextView android:id="@+id/userName"   
  17.             android:layout_width="80dip"  
  18.             android:layout_height="30dip"   
  19.             android:textSize="10pt"  
  20.             android:singleLine="true"/>  
  21.         <TextView android:id="@+id/userScore"   
  22.             android:layout_width="80dip"  
  23.             android:layout_height="30dip"   
  24.             android:textSize="10pt"  
  25.             android:singleLine="true"/>  
  26.   </LinearLayout>  

scorelist.xml

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >
      <LinearLayout 
          android:id="@+id/listLinearLayout"
          android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ListView 
            android:id="@id/android:list"
             android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
      </LinearLayout>
  </LinearLayout>


public class ScoreListActivity extendsListActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scorelist);
        
        ScoreDao scoreDao=new ScoreDao(ScoreListActivity.this);
        List<UserScore> userScores=scoreDao.getAllScore();
        ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
        for (UserScore userScore : userScores) {
            HashMap <String,Object> map=new HashMap <String,Object>();
            map.put("scoreId", userScore.getId());
            map.put("userName", userScore.getUserName());
            map.put("userScore", userScore.getUserScore());
            list.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(ScoreListActivity.this,
                list,R.layout.item, new String[]{"scoreId","userName","userScore"},
                new int[]{R.id.scoreId,R.id.userName,R.id.userScore});
        setListAdapter(adapter);      
        
    }
    //重写ListActivity中的onListItemClick()方法
    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
        HashMap<String, Object> item = (HashMap<String, Object>)listView.getItemAtPosition(position);
        //获取被选中行对应的ID
        Object scoreId=item.get("scoreId");
        Toast.makeText(ScoreListActivity.this,scoreId.toString(), Toast.LENGTH_LONG).show();
    }

}

转自http://blog.csdn.net/guoquanyou/article/details/7075637

原创粉丝点击