androdi listview的应用

建立一个布局xml:message_lists.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical"
    android:background="#fff" >  
    
  
    <ListView  
     	android:id="@+id/mylistview"
     	android:layout_width="fill_parent" 
     	android:layout_height="wrap_content" />
        
  
</LinearLayout>  
建立listview的模板xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   	 	xmlns:tools="http://schemas.android.com/tools"  
        android:id="@+id/message"  
        android:layout_width="fill_parent"
        android:layout_height="51dp"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:background="#fff" >

        <TextView
            android:id="@+id/tv_message_id"
            android:layout_width="25dp"
            android:layout_height="51dp"
            android:text="1"
            android:textColor="#000"
            android:textSize="15sp"
            android:textStyle="normal"
            android:gravity="center"
            android:fontFamily="微软雅黑" />

        <TextView
            android:id="@+id/tv_message_information"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/tv_message_id"
            android:fontFamily="微软雅黑"
            android:gravity="center"
            android:text="我的内容我的内容我的内容我的15"
            android:textColor="#000"
            android:textSize="15sp"
            android:textStyle="normal" />
        
        <ImageView
            android:id="@+id/iv_message_state"
            android:layout_width="31dp"
            android:layout_height="31dp"
            android:paddingTop="15dp"
            android:layout_marginRight="10dp"
			android:layout_alignParentRight="true"
            android:src="@drawable/state1" />

        <TextView
            android:id="@+id/tv_settime"
            android:layout_width="wrap_content"
            android:layout_height="15dp"
            android:layout_alignLeft="@+id/tv_message_information"
            android:layout_below="@+id/tv_message_information"
            android:text="时间"
            android:textColor="#999999"
            android:textSize="12sp"
            android:textStyle="normal" />

    </RelativeLayout >
效果如下:

activity中:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
<span style="white-space:pre">		</span>//绑定listview xml
		setContentView(R.layout.message_lists);

		//显示ListView
		ListView mylistview = (ListView) findViewById(R.id.mylistview);
		MyBaseAdapter myadapter = new MyBaseAdapter(this);
        <span style="white-space:pre">	</span>mylistview.setAdapter(myadapter);
	}
BaseAdapter应用

class MyBaseAdapter extends BaseAdapter {
		private Context context;//用于接收传递过来的Context对象
//        private String inflater = Context.LAYOUT_INFLATER_SERVICE;
		private List<Hyhealth> hyhealthList;
		
		public MyBaseAdapter(Context context) {
            this.context = context;
            //从数据库取出数据
            hyhealthList = new ArrayList<Hyhealth>();
         	SQLiteDAOImpl dataBaseService = new SQLiteDAOImpl(MainActivity.this);
         	hyhealthList = dataBaseService.selectAll();
        }
		 public int getCount() {
            return hyhealthList.size();
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        	LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        	//取出listview的模板xml
        	View view = inflater.inflate(R.layout.message_item, null);
        	//设置id
        	String id = String.valueOf(position+1);
            final TextView tv_message_id = (TextView) view.findViewById(R.id.tv_message_id);
            tv_message_id.setText(id);
            //设置information
            String information = hyhealthList.get(position).getDescription();
            if(information.length()>20){
            	information = information.substring(0, 15)+"...";
            }
            final TextView tv_message_information = (TextView) view.findViewById(R.id.tv_message_information);
            tv_message_information.setText(information);
            //设置time
            String time = hyhealthList.get(position).getSaveTime();
            final TextView tv_settime = (TextView)view.findViewById(R.id.tv_settime);
            tv_settime.setText(time);
            //设置state的ImageView
            ImageView iv_message_state = (ImageView) view.findViewById(R.id.iv_message_state);
            if(hyhealthList.get(position).getState().equals("1")){
            	iv_message_state.setImageResource(R.drawable.state1);
            }
            else if(hyhealthList.get(position).getState().equals("2")){
            	iv_message_state.setImageResource(R.drawable.state3);
            }
            else if(hyhealthList.get(position).getState().equals("3")){
            	iv_message_state.setImageResource(R.drawable.state3);
            }
            //RelativeLayout的点击事件
            RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.message);
        	relativeLayout.setOnClickListener(new OnClickListener() 
				{
				@Override
				public void onClick(View v) {
					int tableId = hyhealthList.get(Integer.valueOf(tv_message_id.getText().toString())-1).getId();
					Intent intent = new Intent();
					intent.setClass(MainActivity.this, ResultActivity.class);
					intent.putExtra("TableID", String.valueOf(tableId));
					startActivity(intent);
					finish();
					}
				});
            return view;
        }
    }
这样就循环完毕了,测试时可以做简单点

效果如下:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值