Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 第一章,listview的簡易用法(Android)

第一章,listview的簡易用法(Android)

編輯:關於Android編程

這篇文章主要是練習了安卓listview的arrayadapter和baseadapter的簡單填充。

1.arrayadapter填充

布局:



    


代碼:

 

package com.example.demo02;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView main_listview;

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

		// 初始化控件和數據
		initView();
	}

	private void initView() {

		main_listview = (ListView) this.findViewById(R.id.main_listview);

		// 初始化數據
		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1);

		for (int i = 0; i < 20; i++) {
			adapter.add("item-->" + i);
		}
		
		// 為listview添加item
		main_listview.setAdapter(adapter);
		
	}

}
調試截圖:

 

\

 

2.baseadapter填充

布局:

 



    
    

mainactivity:

 

 

package com.example.demo03;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView main_listview;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
//        初始化數據
        initView();
    }

	private void initView() {
		// TODO Auto-generated method stub
		
		main_listview = (ListView) this.findViewById(R.id.main_listview);
		
		MainAdapter adapter = new MainAdapter(this);
		
		main_listview.setAdapter(adapter);
		
	}
    
}

 

mainadapter:

 

package com.example.demo03;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MainAdapter extends BaseAdapter {

	private Context context;
	
	public MainAdapter(Context context) {
		this.context = context;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 10;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		
		TextView tv = new TextView(context);
		tv.setText("item···>"+arg0);
		
		return tv;
	}

}
調試截圖:

 

\
 


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