自定义适配器()MainActivity

作者在 2015-12-28 23:07:10 发布以下内容
public class MainActivity extends Activity {

	private ListView listView;
	private List<User> userList;
	private List<String> list;
	private List<String> list2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) this.findViewById(R.id.listView1);

		// 单行文字绑定
		OneTextAdapter1 adapterdata = new OneTextAdapter1(getOneTextData(),
				this);

		// 单行图片绑定
		// OneImageAdater1 adapterdata = new
		// OneImageAdater1(getImageByClassData(), this);

		// 对象绑定
		// ObjectMyAdater adapterdata = new
		// ObjectMyAdater(this,getObjectData());

		listView.setAdapter(adapterdata);

		listView.setOnItemClickListener(new ObjectOnClickItemsListenters());// 对象适配监听

	}

		// 点击Item跳转
	class ObjectOnClickItemsListenters implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int id,
				long arg3) {
			User u = userList.get(id);
			Intent it = new Intent();
			Bundle b = new Bundle();
			b.putSerializable("user", u);
			it.putExtras(b);

			it.setClass(MainActivity.this, TestActivity.class);
			MainActivity.this.startActivity(it);
			// Toast.makeText(MainActivity.this, u.getName(), 2500).show();

		}

	}

	// 继承对象的Adapter
	class ObjectMyAdater extends ObjectAdapter {

		public ObjectMyAdater(Context context, List<User> list) {
			super(context, list);
			// TODO Auto-generated constructor stub
		}

	}


	// 单文字继承Adapter
	class OneTextAdapter1 extends OneTextAdapter {

		public OneTextAdapter1(List<String> list, Context context) {
			super(list, context);
		}

	}

	// 继承图片的Adapter
	class OneImageAdater1 extends OneImageAdapter {

		public OneImageAdater1(List<Integer> list, Context context) {
			super(list, context);
			// TODO Auto-generated constructor stub
		}

	}

	// 单行文字
	public List<String> getOneTextData() {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 4; i++) {
			list.add("数据" + i);
		}
		return list;
	}

	// 反射图片
	public List<Integer> getImageByClassData() {
		List<Integer> list = new ArrayList<Integer>();
		Class cls = R.drawable.class;
		Field[] fields = cls.getDeclaredFields();
		try {
			// 循环添加
			for (Field f : fields) {
				// 判断不是第一张图片
				f.setAccessible(true);
				if (f.getName().indexOf("ic_") < 0) {
					list.add(f.getInt(R.drawable.class));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	// 面向对象
	public List<User> getObjectData() {

		for (int i = 0; i < 3; i++) {
			User u = new User("张三" + i, 20 + i);
			userList.add(u);
		}
		return userList;
	}

	@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;
	}

}
android | 阅读 1846 次
文章评论,共0条
游客请输入验证码
浏览239590次
最新评论