listView 简单测试1

2016-8-21 流沙 Android

package com.itheima.studentsystem;

import java.util.List;

import com.itheima.studentsystem.domain.Student;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText ed_name;
	private RadioGroup rgb;
	private ListView lv;
	
	private StudentDao sdao;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//初始化
		ed_name = (EditText) findViewById(R.id.ed_name);
		rgb= (RadioGroup) findViewById(R.id.rgb);
		lv = (ListView) findViewById(R.id.lv);
		sdao = new StudentDao(this);
		
		//refreshView();
		lv.setAdapter(new MyAdapter());
		
	}
	
	public void save(View v){
		
		String name = ed_name.getText().toString().trim();
		
		if(TextUtils.isEmpty(name)){
			Toast.makeText(this, "学生信息不能为空", 0).show();
			return;
		}
		
		String sex ="male";
		
		int id = rgb.getCheckedRadioButtonId();
		if(id==R.id.male){
			sex ="male";
		}else{
			sex ="female";
		}
		
		//将数据 保存到数据库 中去 
		
		// 拿到 dao 
		
		Student st = new Student("xjlkj",name,sex);
		sdao.add(st);
		Toast.makeText(this, "保存  "+ name+"成功", 0).show();
		//将 所有的 数据 同步的显示 到 屏幕上去 
		
		//查询 现有的数据
		lv.setAdapter(new MyAdapter());
	}

	
	class MyAdapter extends BaseAdapter{
		
		public List<Student> students = sdao.getAll();
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			int len = students.size();
			//System.out.println("len = "+len);
			
			return len;
		}
		
		/*
		 
		  View v;
        if (convertView == null) {
            v = newView(mContext, mCursor, parent);
        } else {
            v = convertView;
        }
		 
		 */
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			  View view;
		      if (convertView == null) {
		        view = View.inflate(MainActivity.this, R.layout.item, null);
		      } else {
		        view = convertView;
		       }

			//View view = View.inflate(MainActivity.this, R.layout.item, null);
			ImageView iv = (ImageView)view.findViewById(R.id.item_iv);
			TextView tv = (TextView)view.findViewById(R.id.item_tv);
			Student student = students.get(position);
			String sex = student.getSex();
			if("male".equals(sex))
			{
				iv.setImageResource(R.drawable.nan);
			}else{
				iv.setImageResource(R.drawable.nv);
			}
			
			tv.setText(student.getName()+",位置:"+position);
			//System.out.println(view.toString());
			return view;
		}

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

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

		
		
		
	}
	
//	private void refreshView() {
//		
//		// 将现有的 全部给清空一下 
//		//ll.removeAllViews();
//		lv.removeAllViews();
//		
//		
//	}
}

标签: listview

发表评论:

Powered by 流沙团

备案号:鄂ICP备15017378号-1