Android列表视图数据加载

效果图:

Android列表视图数据加载_第1张图片

实现思路:

1.创建ListView布局,以及展示数据的视图

2.准备数据

3.创建自定义适配器(将准备好的数据绑定到要展示的视图文件中)

4.为ListView组件设置创建好的适配器

1.视图模块

ListView布局:




    

展示数据的视图:




    

    

        

        

            

            
        

        

            

            
        

        

            

            
        

        

            

            
        

        
	
            

            
        
    

2.数据模块(这里是进行数据模拟,实际数据是来自数据库)

实体类:

package com.study.t212_09;

public class Book {
	private Integer id;
	private String title;
	private String author;

	private Float price;
	private String publish;
	private String remark;

	private int image;

	public Book() {
		super();
	}

	public int getImage() {
		return image;
	}

	public void setImage(int image) {
		this.image = image;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public String getPublish() {
		return publish;
	}

	public void setPublish(String publish) {
		this.publish = publish;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

}

数据准备:

package com.study.t212_09;

import java.util.ArrayList;
import java.util.List;

public class BookDao {

	private int[] bookImages = new int[] { R.drawable.book1, R.drawable.book2,
			R.drawable.book3, R.drawable.book4, R.drawable.book5,
			R.drawable.book6, R.drawable.book7, R.drawable.book8,
			R.drawable.book9, R.drawable.book10 };

	public List list() {
		List list = new ArrayList();
		for (int i = 1; i <= 1000; i++) {
			Book b = new Book();
			b.setId(i);
			b.setTitle("t" + i);
			b.setAuthor("a" + i);
			b.setPrice(0.0f + i);
			b.setPublish("p" + i);
			b.setRemark("r" + i);
			b.setImage(bookImages[i % bookImages.length]);

			list.add(b);
		}

		return list;
	}
}

3.实现

package com.study.t212_09;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    private List data;
    private MyBaseAdapt mba;//自定义适配器
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = findViewById(R.id.list_view);

        //1.数据准备
        data = new BookDao().list();
        //2.创建自定义适配器
        mba = new MyBaseAdapt((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        //3.为listView设置适配器
        lv.setAdapter(mba);
    }

    public class MyBaseAdapt extends BaseAdapter{
        public class ViewHolder{
            ImageView iv_listviewitem_image;
            TextView tv_listviewitme_title;
            TextView tv_listviewitme_author;
            TextView tv_listviewitme_price;
            TextView tv_listviewitme_publish;
            TextView tv_listviewitme_remark;
        }

        private LayoutInflater inflater;

        public MyBaseAdapt(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = view;
            if (v == null){
                v = inflater.inflate(R.layout.listview_item,null);
                ViewHolder vh = new ViewHolder();
                vh.iv_listviewitem_image = v.findViewById(R.id.iv_listviewitem_image);
                vh.tv_listviewitme_title = v.findViewById(R.id.tv_listviewitme_title);
                vh.tv_listviewitme_author = v.findViewById(R.id.tv_listviewitme_author);
                vh.tv_listviewitme_price = v.findViewById(R.id.tv_listviewitme_price);
                vh.tv_listviewitme_publish = v.findViewById(R.id.tv_listviewitme_publish);
                vh.tv_listviewitme_remark = v.findViewById(R.id.tv_listviewitme_remark);
                v.setTag(vh);
            }

            ViewHolder vh = (ViewHolder) v.getTag();
            Book book = data.get(i);
            vh.iv_listviewitem_image.setImageResource(book.getImage());
            vh.tv_listviewitme_title.setText(book.getTitle());
            vh.tv_listviewitme_author.setText(book.getAuthor());
            vh.tv_listviewitme_price.setText(book.getPrice()+"");
            vh.tv_listviewitme_publish.setText(book.getPublish());
            vh.tv_listviewitme_remark.setText(book.getRemark());
            return v;
        }
    }
}

 

你可能感兴趣的:(android)