Android中的使用ListView控件显示List集合中的数据,并对Item响应选择事件(方法一)
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();
}
});
}
}
更多相关文章
- 在Unity中捕捉Android的常用按钮返回事件
- sencha touch中处理Android返回按钮事件
- Android:控件Spinner、getResources、setDropDownViewResource、s
- Android的事件响应机制
- 安卓事件分发流程
- Android改变图片颜色的自定义控件(十)
- 第七篇 ScrollView控件
- android控件 ToggleButton的应用
- 整理的Android多种控件步骤