Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習之路之ListView的使用

Android學習之路之ListView的使用

編輯:關於Android編程

學習android三天了,發現這個ListView在android裡應用非常的多,於是就花了一些時間仔細學習了一下!

以下是我個人的理解,如果有錯誤或不周到的地方,還請各位看客留言!有錯誤才有進步,這是我的名言!!!呵呵!

簡單的介紹以下,ListVeiw就像一個對象集合,可以將數據一列一列的顯示出來,而且可以添加點擊事件,非常方便用戶操作

手機自帶的系統用的比較多

比如手機的設置界面:

\

*布局設計<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+1eLR+bXEvefD5tDo0qrBvbj2TGF5b3V01sbX96Os0ru49r7NysdNYWluTGF5b3V0o6zA78PmsPy6rNK7uPZMaXN0VmlldzwvcD4KPHA+tPrC68jnz8KjujwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;"> 這個就相當於一個空集合

下面是PerListViewLayout,裡面是每一條數據的樣式

代碼如下:


	
    
    
    
    
        
        
    
對應的界面如下:

\
大概可以分成這幾個部分:

\

這個可以成為每一列數據的樣式與布沮喎?/kf/yidong/wp/" target="_blank" class="keylink">WPC9wPgo8cD7S1LrzTWFpbkxheW91dMDvw+a1xMO/zPXK/b7dtry74dLU1eLR+bXEt73Kvc/Uyr6z9sC0PC9wPgo8cD4qIMr9vt3UtLXEtLS9qDxicj4KytfPyNKqyKGz9sr9vt2jrLyw0qrP1Mq+vLjM9cr9vt08L3A+CjxwPrSpvP7I/bj2yv3X6aOst9ax8LTmyOtpbWFnZVZpZXehonRleHRWaWV3MaGidGV4dFZpZXcyPC9wPgo8cD60+sLryOfPwqO6PC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">private int[] picts = {R.drawable.ic_launcher, R.drawable.pict1, R.drawable.pict2, R.drawable.dog_bew}; private String[] contents1 = {"zhangsan", "lisi", "wangwu", "wangliu"}; private String[] contents2 = {"zhangsan_1", "lisi_1", "wangwu_1", "wangliu_1"};圖片資源的id,textView1的內容,textView2的內容。一一對應

將數據存入List裡

代碼如下:

// 創建數據源
		List> data;
		data = new ArrayList>();
		for (int i = 0; i < picts.length; i++)
		{
			Map oneLine = new HashMap();
			oneLine.put("pict", picts[i]);
			oneLine.put("contents1", contents1[i]);
			oneLine.put("contents2", contents2[i]);
			
			data.add(oneLine);
		}

這裡用List>最好

開始創建自定義適配器

代碼如下:

	class MyAdapter extends SimpleAdapter
	{

		public MyAdapter(Context context, List> data,
				int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view =  super.getView(position, convertView, parent);
			
			ImageView imageview = (ImageView)view.findViewById(R.id.imageViewId);
			TextView textview1 = (TextView)view.findViewById(R.id.textviewId1);
			TextView textview2 = (TextView)view.findViewById(R.id.textviewId2);
			imageview.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Toast.makeText(ListViewActivity3.this, "點擊的是圖片", Toast.LENGTH_SHORT).show();
				}
			});
			textview1.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Toast.makeText(ListViewActivity3.this, "點擊的是文字1", Toast.LENGTH_SHORT).show();
				}
			});
			textview2.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Toast.makeText(ListViewActivity3.this, "點擊的是文字2", Toast.LENGTH_SHORT).show();
				}
			});
			// 實現事件。。。
			return view;
		}
		
		
	}
注意上面的view,事件是點擊MainLayout獲取PerListViewLayout裡的各個控件的id和文本
創建適配器

代碼如下:

	// 創建適配器
		MyAdapter adapter = new MyAdapter(ListViewActivity3.this, data, R.layout.listview_one_linex, from, to);
		listview.setAdapter(adapter);
		
		

		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1, int position,
					long arg3) {
				Toast.makeText(ListViewActivity3.this, "第"+ (position+1) + "行", Toast.LENGTH_SHORT).show();
				
			}
		});
開始看看from和to參數,

context - The context where the View associated with this SimpleAdapter is running
data - A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entri	es specified in "from"
resource - Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defin	ed in "to"
from - A list of column names that will be added to the Map associated with each item.
to - The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of t	he first N columns in the from parameter.
英文看不懂,自己看吧,我的理解就是 一一對應關系
這樣定義:

private String[] from = {"pict","contents1","contents2"};
	private int[] to = {R.id.imageViewId, R.id.textviewId1, R.id.textviewId2};

代碼運行起來的結果

\

下面是我做的圖

這個是我自己的理解,肯定有很多地方不合理,我自己是懂了!!!!




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved