ListView的使用

2018-05-12  本文已影响0人  Summer_27d1

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<String> adapter=new ArrayAdapter<String>
    (this, R.layout.item_array,data);
    //设置adapter显示列表
    lv_main.setAdapter(adapter);
}

}

···
---------xml 中---------------
···
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="20sp"
android:gravity="center_vertical"
>

</TextView>

···
******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<Map<String, Object>> data = new ArrayList<Map<String,Object>>();

    HashMap<String, Object> mp = new HashMap<String, Object>();
    mp.put("icon", R.drawable.f1);
    mp.put("name", "美食---1");
    mp.put("content", "内容--1");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon", R.drawable.f2);
    mp.put("name", "美食---2");
    mp.put("content", "内容--2");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon", R.drawable.f3);
    mp.put("name", "美食---3");
    mp.put("content", "内容--3");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon",R.drawable.f4);
    mp.put("name", "美食---4");
    mp.put("content", "内容--4");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon", R.drawable.f5);
    mp.put("name", "美食---5");
    mp.put("content", "内容--5");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon", R.drawable.f6);
    mp.put("name", "美食---6");
    mp.put("content", "内容--6");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon", R.drawable.f7);
    mp.put("name", "美食---7");
    mp.put("content", "内容--7");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon", R.drawable.f8);
    mp.put("name", "美食---8");
    mp.put("content", "内容--8");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    mp.put("icon", R.drawable.f9);
    mp.put("name", "美食---9");
    mp.put("content", "内容--9");
    data.add(mp);
    
    mp=new HashMap<String, Object>();
    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*********
···
<?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="horizontal" >

<ImageView
    android:id="@+id/iv_item_icon"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:src="@drawable/f1" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_marginLeft="10dp"
     >

    <TextView
        android:id="@+id/iv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        
        />

    <TextView
        android:id="@+id/iv_item_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

</LinearLayout>
···
。。。。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<ShoppInfo> 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<ShoppInfo>();
    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******
···
<?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="horizontal" >

<ImageView
    android:id="@+id/iv_item_icon"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:src="@drawable/f1" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_marginLeft="10dp"
     >

    <TextView
        android:id="@+id/iv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        
        />

    <TextView
        android:id="@+id/iv_item_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

</LinearLayout>
···
/////////////////////进一步优化////////////////////
需要创建一个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<ShoppInfo> 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<ShoppInfo>();
    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;

    }

}

}

上一篇下一篇

猜你喜欢

热点阅读