ListView的使用

ListView 是用来显示多个可以滑动项列表的ViewGroup
显示列表:
listView.setAdapter(adpter)
更新列表:aspater.notifyDataSetChanged


image.png
image.png

image.png

-----------ListView+ArrayAdapter---------
-------------------效果图如下---------------


image.png

------------------代码如下---------------

···
package com.example.test08_listvieew;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    lv_main=(ListView) findViewById(R.id.lv_main);
    //准备集合 
    String[] data={"A","B","C","D","E","F","G","H","J","K","L","O"};
    
    //准备适配器ArrayAdapter对象
    ArrayAdapter adapter=new ArrayAdapter
    (this, R.layout.item_array,data);
    //设置adapter显示列表
    lv_main.setAdapter(adapter);
}

}

···
---------xml 中---------------
···

android:layout_width="match_parent"
android:layout_height="50dp"
android:text="20sp"
android:gravity="center_vertical"
>

···
******ListView+SimpleAdatper*******
------------效果图如下---------------------


image.png

********************代码如下***************
···
package com.example.test08_listvieew;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity_Array extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    lv_main=(ListView) findViewById(R.id.lv_main);
    //准备集合 
    List> data = new ArrayList>();

    HashMap mp = new HashMap();
    mp.put("icon", R.drawable.f1);
    mp.put("name", "美食---1");
    mp.put("content", "内容--1");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f2);
    mp.put("name", "美食---2");
    mp.put("content", "内容--2");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f3);
    mp.put("name", "美食---3");
    mp.put("content", "内容--3");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon",R.drawable.f4);
    mp.put("name", "美食---4");
    mp.put("content", "内容--4");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f5);
    mp.put("name", "美食---5");
    mp.put("content", "内容--5");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f6);
    mp.put("name", "美食---6");
    mp.put("content", "内容--6");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f7);
    mp.put("name", "美食---7");
    mp.put("content", "内容--7");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f8);
    mp.put("name", "美食---8");
    mp.put("content", "内容--8");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f9);
    mp.put("name", "美食---9");
    mp.put("content", "内容--9");
    data.add(mp);
    
    mp=new HashMap();
    mp.put("icon", R.drawable.f10);
    mp.put("name", "美食---10");
    mp.put("content", "内容--10");
    data.add(mp);
    
    //map对象的key数组 ,用于得到对应的value
    String[] from={"icon","name","content"};
    //item布局文件中的子View的id的数组
    int[] to={R.id.iv_item_icon,R.id.iv_item_name,R.id.iv_item_content};
    
    //准备SimpleAdapter对象
    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter, from, to);
    //设置Adapter显示列表
    lv_main.setAdapter(adapter);
}

}

···
**************XML*********
···

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >





    

    



···
。。。。ListView+BaseAdapter。。



首先,创建一个类shoppInfo存储商品信息

-------代码如下--------------
···
package com.example.test08_listvieew;

public class ShoppInfo {
private int icon;
private String name;
private String content;
public ShoppInfo() {
super();
}
public ShoppInfo(int icon, String name, String content) {
super();
this.icon = icon;
this.name = name;
this.content = content;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "ShoppInfo [icon=" + icon + ", name=" + name + ", content="
+ content + "]";
}

}

···

******效果图**********


image.png

*********代码如下******
···
package com.example.test08_listvieew;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class MainActivity_Simple extends Activity {
private ListView lv_main;
private List data;//商品信息数据源
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    lv_main=(ListView) findViewById(R.id.lv_main);
    //准备集合数据
    data=new ArrayList();
    data.add(new ShoppInfo(R.drawable.f1,"美食名称--1","美食的内容---1"));
    data.add(new ShoppInfo(R.drawable.f2,"美食名称--2","美食的内容---2"));
    data.add(new ShoppInfo(R.drawable.f3,"美食名称--3","美食的内容---3"));
    data.add(new ShoppInfo(R.drawable.f4,"美食名称--4","美食的内容---4"));
    data.add(new ShoppInfo(R.drawable.f5,"美食名称--5","美食的内容---5"));
    data.add(new ShoppInfo(R.drawable.f6,"美食名称--6","美食的内容---6"));
    data.add(new ShoppInfo(R.drawable.f7,"美食名称--7","美食的内容---7"));
    data.add(new ShoppInfo(R.drawable.f8,"美食名称--8","美食的内容---8"));
    data.add(new ShoppInfo(R.drawable.f9,"美食名称--9","美食的内容---9"));
    data.add(new ShoppInfo(R.drawable.f10,"美食名称--10","美食的内容---10"));
    
    //准备适配器对象   BaseAdatper
    MyAdapter adapter = new MyAdapter();
    //
    lv_main.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{

    @Override//返回集合的总数
    public int getCount() {
        return data.size();
    }

    @Override//返回指定下标对应的数据对象
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override//返回每个条目的id 下标
    public long getItemId(int position) {
        return position;
    }

    @Override//返回指定下标所对应(Item)的视图对象
    //position 下标           parent  listView对象
    //convertView 可复用Item的视图对象   前n+1个为null
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null) {
            //加载条目(Item)布局 得到视图(View)对象
            convertView= View.inflate(MainActivity_Simple.this,
                    R.layout.item_simple_adapter, null);
        }
        //得到当前行的商品信息                               根据postion设置对应的数据
        ShoppInfo shoppInfo = data.get(position);
        //得到子View对象
        ImageView inv = (ImageView) convertView.findViewById(R.id.iv_item_icon);
        TextView inv1 = (TextView) convertView.findViewById(R.id.iv_item_name);
        TextView inv2 = (TextView) convertView.findViewById(R.id.iv_item_content);
        
        //设置数据
        inv.setImageResource(shoppInfo.getIcon());
        inv1.setText(shoppInfo.getName());
        inv2.setText(shoppInfo.getContent());
        
        return convertView;
    }
    
    
}

}

···
********XML******
···

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >





    

    



···
/////////////////////进一步优化////////////////////
需要创建一个ViewHolder
·---------------代码如下------------
···

···package com.example.test08_listvieew;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class CopyOfMainActivity_Simple extends Activity {
private ListView lv_main;
private List data;//商品信息数据源
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    lv_main=(ListView) findViewById(R.id.lv_main);
    //准备集合数据
    data=new ArrayList();
    data.add(new ShoppInfo(R.drawable.f1,"美食名称--1","美食的内容---1"));
    data.add(new ShoppInfo(R.drawable.f2,"美食名称--2","美食的内容---2"));
    data.add(new ShoppInfo(R.drawable.f3,"美食名称--3","美食的内容---3"));
    data.add(new ShoppInfo(R.drawable.f4,"美食名称--4","美食的内容---4"));
    data.add(new ShoppInfo(R.drawable.f5,"美食名称--5","美食的内容---5"));
    data.add(new ShoppInfo(R.drawable.f6,"美食名称--6","美食的内容---6"));
    data.add(new ShoppInfo(R.drawable.f7,"美食名称--7","美食的内容---7"));
    data.add(new ShoppInfo(R.drawable.f8,"美食名称--8","美食的内容---8"));
    data.add(new ShoppInfo(R.drawable.f9,"美食名称--9","美食的内容---9"));
    data.add(new ShoppInfo(R.drawable.f10,"美食名称--10","美食的内容---10"));

    //准备适配器对象   BaseAdatper
    MyAdapter adapter = new MyAdapter();
    //
    lv_main.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{

    @Override//返回集合的总数
    public int getCount() {
        return data.size();
    }

    @Override//返回指定下标对应的数据对象
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override//返回每个条目的id 下标
    public long getItemId(int position) {
        return position;
    }

    @Override//返回指定下标所对应(Item)的视图对象
    //position 下标           parent  listView对象
    //convertView 可复用Item的视图对象   前n+1个为null
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler  holder=null;
        //如果没有复用
        if (convertView==null) {
            //加载条目(Item)布局 得到视图(View)对象
            convertView= View.inflate(CopyOfMainActivity_Simple.this,
                    R.layout.item_simple_adapter, null);
            holder=new ViewHodler();
            
            holder.inv = (ImageView) convertView.findViewById(R.id.iv_item_icon);
            holder.nametext = (TextView) convertView.findViewById(R.id.iv_item_name);
            holder.inv2 = (TextView) convertView.findViewById(R.id.iv_item_content);
            //将holder 保存在convertView上
            convertView.setTag(holder);
        }else{
            //复用时     取出holder
            holder=(ViewHodler) convertView.getTag();   
        }
        //2. 得到当前行的对象
        ShoppInfo shoppInfo = data.get(position);
        //3.给ViewHolder对象的数据设置数据
        holder.inv.setImageResource(shoppInfo.getIcon());
        holder.nametext.setText(shoppInfo.getName());
        holder.inv2.setText(shoppInfo.getContent());
        return convertView;
    }//视图容器类
    class ViewHodler{
        public ImageView inv;
        public TextView  nametext;
        public TextView  inv2;

    }

}

}

你可能感兴趣的:(ListView的使用)