Android中的使用ListView控件显示List集合中的数据,并对Item响应选择事件(方法一)_第1张图片

item.xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingLeft="10dp"    android:paddingRight="10dp"    android:paddingTop="1dp"    android:paddingBottom="1dp"    xmlns:android="http://schemas.android.com/apk/res/android" >      <TextView           android:id="@+id/scoreId"          android:layout_width="60dip"      android:layout_height="30dip"       android:textSize="10pt"      android:singleLine="true"/>      <TextView android:id="@+id/userName"           android:layout_width="80dip"      android:layout_height="30dip"       android:textSize="10pt"      android:singleLine="true"/>      <TextView android:id="@+id/userScore"           android:layout_width="80dip"      android:layout_height="30dip"       android:textSize="10pt"      android:singleLine="true"/>  </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/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>

public class ScoreListActivity extendsActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scorelist);

ListView listView=(ListView) findViewById(R.id.listView);
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});

listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//parent是被点击的那个ListView
ListView listView = (ListView)parent;
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();

}
});
}
}


更多相关文章

  1. 在Unity中捕捉Android的常用按钮返回事件
  2. sencha touch中处理Android返回按钮事件
  3. Android:控件Spinner、getResources、setDropDownViewResource、s
  4. Android的事件响应机制
  5. 安卓事件分发流程
  6. Android改变图片颜色的自定义控件(十)
  7. 第七篇 ScrollView控件
  8. android控件 ToggleButton的应用
  9. 整理的Android多种控件步骤

随机推荐

  1. shell脚本中mysqldump的基本使用
  2. Navicat连接mysql8出现1251错误
  3. 根据mysql中的另一列获取百分比列
  4. Mysql存入int查找时使用string
  5. MySQL高性能主从架构的复制原理及配置详
  6. 确定记录出现的次数
  7. 整合Hadoop2.2.0+HBase0.96+Hive0.12+MyS
  8. linux下mysql配置文件my.cnf详解
  9. mysql获取某个范围内的随机数,写了个存储
  10. 吐槽:MySQL和Delphi2010的UTF8编码