android常见三个适配器

首先给出整体目录结构android常见三个适配器_第1张图片



设计整个程序是通过主界面的三个按钮跳转到三个界面,分别是用BaseAdapte,SimpleAdapter,ArrayAdatper显示ListView

主界面如下


android常见三个适配器_第2张图片



点击任何一个按钮则跳转相应界面,三个界面差不多,只是id设置不同,其中一个界面如图:


android常见三个适配器_第3张图片


现在给出全部布局文件代码:

activity_main


    
    

activity_array


    


activity_base


    

activity_simple

    


然后Lsitview里面的布局是listview.xml:



    



  然后是几个主要界面代码:

主界面:

package com.example.myadapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle icicle) {

		super.onCreate(icicle);
		setContentView(R.layout.activity_main);
		Button button1 = (Button) findViewById(R.id.button1);
		Button button2 = (Button) findViewById(R.id.button2);
		Button button3 = (Button) findViewById(R.id.button3);
		button1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,
						BaseActivity.class);
				startActivity(intent);
			}
		});

		button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,
						SimpleActivity.class);
				startActivity(intent);
			}
		});
		button3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,
						ArrayActivity.class);
				startActivity(intent);
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
BaseActivity:


package com.example.myadapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class BaseActivity extends Activity {
	private String []nameStrings={"第一次","第二次","第三次"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_base);
		ListView listView=(ListView)findViewById(R.id.base);
		myBaseAdapter myBaseAdapter=new myBaseAdapter();
		listView.setAdapter(myBaseAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.base, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	class myBaseAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return nameStrings.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return nameStrings[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
			View view=View.inflate(BaseActivity.this, R.layout.listview, null);
	//listview是ListVIew的具体布局文件的xml文件
return view
}

 SimpleActivity: 
  

package com.example.myadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class SimpleActivity extends Activity {
	private String []nameStrings={"第一次","第二次","第三次"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_simple);
		
		 List> listems = new ArrayList>();  
	        for (int i = 0; i < nameStrings.length; i++) {  
	            Map listem = new HashMap();  
	            listem.put("head", nameStrings[i]);  
	           
	            listems.add(listem);  
	        }  
	        
	        SimpleAdapter simplead = new SimpleAdapter(this, listems,  
	               R.layout.listview, new String[] {"head"},  
	                new int[] {R.id.listText});  
	          //第一个参数是上下文,第二个是List>参数,第三个是ListView具体布局ID
	        //第4个是List中的map的key值,第5个是List中的map的key值对应显示的TextView或者其他控件
	        ListView listView=(ListView)findViewById(R.id.simple);  
	        listView.setAdapter(simplead);
	        
	        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.simple, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
ArrayActivity:

package com.example.myadapter;

import java.util.List;

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

public class ArrayActivity extends Activity {
	private String []nameStrings={"第一次","第二次","第三次"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_array);
		ListView listView=(ListView)findViewById(R.id.array);
		ArrayAdapter adapter=new ArrayAdapter(ArrayActivity.this, android.R.layout.simple_expandable_list_item_1,nameStrings);
		listView.setAdapter(adapter);
		//android.R.layout.simple_expandable_list_item_1是系统已经存在的
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.array, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	class myArrayAdapter extends ArrayAdapter{

		public myArrayAdapter(Context context, int resource,
				int textViewResourceId, List objects) {
			super(context,resource, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
		}
		
	}
}

清单文件自动注册各自的activity;






你可能感兴趣的:(android常见三个适配器)