item.xml
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" >
android:id="@+id/scoreId"
android:layout_width="60dip"
android:layout_height="30dip"
android:textSize="10pt"
android:singleLine="true"/>
android:layout_width="80dip"
android:layout_height="30dip"
android:textSize="10pt"
android:singleLine="true"/>
android:layout_width="80dip"
android:layout_height="30dip"
android:textSize="10pt"
android:singleLine="true"/>
scorelist.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
android:id="@+id/listLinearLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
public class ScoreListActivityextendsActivity {
@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 userScores=scoreDao.getAllScore();
ArrayList> list=new ArrayList>();
for (UserScore userScore : userScores) {
HashMap map=new HashMap ();
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 item = (HashMap)listView.getItemAtPosition(position); //获取被选中行对应的ID Object scoreId=item.get("scoreId"); Toast.makeText(ScoreListActivity.this,scoreId.toString(), Toast.LENGTH_LONG).show(); } }); } }