ListView 使用自定义适配器,将自定义xml布局转换成view调整布局

package com.cwj.arraydatapter826;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ListView lv;
	private List<Person> persons;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv = (ListView) findViewById(R.id.lv_test);
		long a = 158581800;
		persons = new ArrayList<Person>();
		for (int i = 0; i < 20; i++) {
			Person p = new Person();
			p.setAge(i);
			p.setName("hh" + i);
			p.setPhone(Long.toString(a + i));
			persons.add(p);
		}
		lv.setAdapter(new MyAdapt());

	}

	private class MyAdapt extends BaseAdapter {

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Person person = persons.get(position);
			View v = View.inflate(MainActivity.this, R.layout.lv_list, null);
			TextView tv_id=(TextView) v.findViewById(R.id.tv_use);
			tv_id.setText(person.getAge()+"");
			TextView tv_name=(TextView) v.findViewById(R.id.tv_name);
			tv_name.setText(person.getName());
			TextView tv_phone=(TextView) v.findViewById(R.id.tv_phone);
			tv_phone.setText(person.getPhone());
			
			return v;
		}

	}
}

其中person类如下

package com.cwj.arraydatapter826;

public class Person {
	private int age;
	private String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	private String phone;
}
lv_list 文件内容如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal" 
    >

    <TextView
        android:id="@+id/tv_use"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:textSize="20sp" 
        android:gravity="center"
        android:textColor="#ff0000"
        android:text="id"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="name"
            android:layout_marginLeft="10dp"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_phone"
            android:textColor="#88000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="phone"
              android:layout_marginLeft="10dp"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

布局界面如下


运行结果如下


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱学习hahaha

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值