zoukankan      html  css  js  c++  java
  • Listview没有优化之前

    MainActivity.java

    package com.example.listviewdemo4;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
    
    	ListView listview;
    	List<Map<String, Object>> list;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		listview = (ListView) findViewById(R.id.listview);
    
    		// 获取数据
    		list = getData();
    
    		listview.setAdapter(new ListViewAdapter(list,MainActivity.this));
    
    	}
    
    	private List<Map<String, Object>> getData() {
    		// TODO Auto-generated method stub
    
    		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("name", "图1");
    		map.put("pictrue", R.drawable.tu1);
    		list.add(map);
    
    		map = new HashMap<String, Object>();
    		map.put("name", "图2");
    		map.put("pictrue", R.drawable.tu2);
    		list.add(map);
    		
    		
    		map = new HashMap<String, Object>();
    		map.put("name", "图3");
    		map.put("pictrue", R.drawable.tu3);
    		list.add(map);
    		
    		
    		map = new HashMap<String, Object>();
    		map.put("name", "图4");
    		map.put("pictrue", R.drawable.tu4);
    		list.add(map);
    		
    		return list;
    	}
    
    }
    

    ListViewAdapter.java

    package com.example.listviewdemo4;
    
    import java.util.List;
    import java.util.Map;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class ListViewAdapter extends BaseAdapter{
    	
    	List<Map<String, Object>> list;
    	Context context;
    	LayoutInflater layoutInflater;
    	ListViewAdapter(List<Map<String, Object>> list,Context context){
    		this.list=list;
    		this.context=context;
    		//使用context上下文获取系统layout_inflater服务
    		layoutInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    		
    	}
    
    	@Override
    	public int getCount() {
    		// TODO Auto-generated method stub
    		return list.size();
    	}
    
    	@Override
    	public Object getItem(int arg0) {
    		// TODO Auto-generated method stub
    		return list.get(arg0);
    	}
    
    	@Override
    	public long getItemId(int arg0) {
    		// TODO Auto-generated method stub
    		return arg0;
    	}
    
    	@Override
    	public View getView(int position, View contentView, ViewGroup arg2) {
    		//layoutInflater 传入list_item会生成一个view
    		View view=layoutInflater.inflate(R.layout.list_item, null);
    //在生成的view中寻找控件 TextView textview=(TextView)view.findViewById(R.id.textview);
    //把控件设置内容 textview.setText((CharSequence) list.get(position).get("name")); ImageView imageView=(ImageView)view.findViewById(R.id.imageview);
    //设置图片的资源 imageView.setImageResource((Integer) list.get(position).get("pictrue")); return view; } }

    配置文件:

     activity_main.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        
        <ListView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:id="@+id/listview"
            ></ListView>
        
        
    
    </RelativeLayout>
    

     listview_item.xml:

    <?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="match_parent"
        android:orientation="vertical" >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview"
            />
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageview"
            />
    
    </LinearLayout>
    

  • 相关阅读:
    Linux学习笔记8——VIM编辑器的使用
    Linux学习笔记7——linux中的静态库和动态库
    Linux学习笔记6——映射虚拟内存
    Linux学习笔记5——虚拟内存
    Linux学习笔记4——函数调用栈空间的分配与释放
    C++中new和malloc
    Linux学习笔记3——Linux中常用系统管理命令
    Linux学习笔记2——Linux中常用文件目录操作命令
    python的基本语法
    11.3 自定义注解
  • 原文地址:https://www.cnblogs.com/childhooding/p/4324999.html
Copyright © 2011-2022 走看看