自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用ListView 顯示數(shù)據(jù)

移動(dòng)開發(fā) Android
在Android中,ListView是用來顯示一個(gè)列表的控件。每一行列表都是一個(gè)獨(dú)立的元素。這種控件既可以方便的顯示從系統(tǒng)中其他應(yīng)用讀取出來的數(shù)據(jù),也可獨(dú)立的為各行元素設(shè)置監(jiān)聽器。

效果圖

1、先定義item

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout   
  3.  xmlns:android="http://schemas.android.com/apk/res/android"   
  4.     android:orientation="horizontal"   
  5.     android:layout_width="fill_parent"   
  6.     android:layout_height="wrap_content"> 
  7.  <TextView   
  8.    android:layout_width="80dip"   
  9.    android:layout_height="wrap_content" 
  10.    android:id="@+id/personid" 
  11.  />   
  12.  <TextView   
  13.    android:layout_width="100dip" 
  14.    android:layout_height="wrap_content" 
  15.    android:id="@+id/name"   
  16. />    
  17.  <TextView   
  18.    android:layout_width="fill_parent"   
  19.    android:layout_height="wrap_content"   
  20.    android:id="@+id/amount"   
  21.  /> 
  22. </LinearLayout> 

2、通過適配器把數(shù)據(jù)綁定到listview中,點(diǎn)擊某一行數(shù)據(jù)顯示編號(hào)

方式一:

  1. List<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();   
  2.        for(Person person :persons){   
  3.          HashMap<String, Object> hashMap=new HashMap<String, Object>();  
  4.          hashMap.put("id", person.getPersonId());  
  5.          hashMap.put("name", person.getName());  
  6.          hashMap.put("amount", person.getAmount());   
  7.          data.add(hashMap);  
  8.  }   
  9. SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.item,new String[]{"id","name","amount"},new int[]{R.id.personid,R.id.name,R.id.amount});              listView.setAdapter(adapter);  
  10.  listView.setOnItemClickListener(new OnItemClickListener(){   
  11. @Override  
  12. public void onItemClick(AdapterView<?> parent, View view,  
  13.    int position, long id) {   
  14.    ListView myListView=(ListView)parent;  
  15.    HashMap<String, Object> item=(HashMap<String, Object>)myListView.getItemAtPosition(position);  
  16.    Toast.makeText(DBActivity.this, item.get("id").toString() , Toast.LENGTH_SHORT).show();  
  17.     }  
  18.   }   
  19. ); 

方式二:用游標(biāo)的方式

  1. Cursor cursor=personService.getCursor(0, 15);        //要求字段中要有_id  設(shè)計(jì)表的時(shí)候的主鍵為_id          
  2. SimpleCursorAdapter cursorAdapter=new SimpleCursorAdapter(this,R.layout.item,cursor,new String[]{"_id","name","amount"},new   int[]{R.id.personid,R.id.name,R.id.amount});    
  3. listView.setAdapter(cursorAdapter);    
  4. listView.setOnItemClickListener(new OnItemClickListener(){   
  5. /**   
  6.  * parent當(dāng)前所點(diǎn)擊的listview對(duì)象   
  7.  * view當(dāng)前所點(diǎn)擊的條目   
  8. */   
  9. @Override   
  10.  public void onItemClick(AdapterView<?> parent, View view,  
  11.    int position, long id) {   
  12.    ListView myListView=(ListView)parent;  
  13.    Cursor data=(Cursor)myListView.getItemAtPosition(position);//根據(jù)位置移動(dòng)游標(biāo)  
  14.    int m=data.getInt(data.getColumnIndex("_id"));   
  15.    Toast.makeText(DBActivity.this, String.valueOf(m) , Toast.LENGTH_SHORT).show();  
  16.     }  
  17.   }  
  18. ); 

 【編輯推薦】

Android智能手機(jī)操作系統(tǒng)

SQL Server與Oracle數(shù)據(jù)庫鏡像對(duì)比

在Android應(yīng)用程序中使用Internet數(shù)據(jù)

在不同字符集的數(shù)據(jù)庫之間導(dǎo)入數(shù)據(jù)的方法

責(zé)任編輯:zhaolei 來源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

2009-08-11 14:12:27

C# ListView

2011-06-03 10:48:23

Android ListView

2011-04-11 13:43:35

popupwindowlistviewAndroid

2009-08-18 09:49:00

C# listview

2011-05-31 11:05:16

ListView 數(shù)據(jù)

2010-01-25 17:53:35

Android Lis

2009-07-01 17:15:25

ListView數(shù)據(jù)排Visual Stud

2009-07-27 16:53:15

ASP.NET 2.0

2011-05-27 15:02:15

Android ListView

2012-04-24 23:31:41

iOS

2014-12-31 14:14:26

AdapterListView List View

2011-05-05 13:30:17

GridViewListView布局

2013-03-27 09:17:17

Android開發(fā)AndroidList

2009-04-07 15:34:00

Linux上網(wǎng)本Windows

2009-07-27 16:37:55

DetailsView

2013-07-17 16:33:02

下拉刷新listvie滾動(dòng)到底部加載Android開發(fā)學(xué)習(xí)

2013-11-26 16:39:21

Android設(shè)計(jì)模式

2009-10-28 09:25:18

VB.NET List

2014-12-17 09:46:30

AndroidListView最佳實(shí)踐

2012-08-20 10:17:03

云服務(wù)加密密鑰云計(jì)算
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)