Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——ListView與適配器

Android——ListView與適配器

編輯:關於Android編程

\

\

\

\

\

\

1.抽屜布局 Drawer

 




    

    

        

            
            
        

    


\

 

\

 

2.ListView新聞列表

 

package com.example.jreduch7292.listdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.jreduch7292.R;

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

public class ListActivity extends AppCompatActivity {
private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        lv=(ListView)findViewById(R.id.lv);
//        List list=new ArrayList();
//        list.add("張三  123");
//        list.add("張四  1234");
//        list.add("張五  12345");
//        list.add("張六  123456");
//        list.add("張三  123");
//        list.add("張四  1234");
//        list.add("張五  12345");
//        list.add("張六  123456");
//        list.add("張三  123");
//        list.add("張四  1234");
//        list.add("張五  12345");
//        list.add("張六  123456");
//        list.add("張三  123");
//        list.add("張四  1234");
//        list.add("張五  12345");
//        list.add("張六  123456");
//        ArrayAdapter aa=new ArrayAdapter(this,
//                android.R.layout.simple_list_item_1,list);
//        lv.setAdapter(aa);
        final List list=new ArrayList();
        Map map=new HashMap();
        map.put("img",R.mipmap.a);
        map.put("name","字母A張三  1121132323");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.b);
        map.put("name","字母B張四  123444434");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.c);
        map.put("name","字母C張五  12334233");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.d);
        map.put("name","字母D張六  11323456");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.e);
        map.put("name","字母E張五  12323445");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.f);
        map.put("name","字母F張四  12324424");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.g);
        map.put("name","字母G張五  12322445");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.h);
        map.put("name","字母H張六  12233456");
        list.add(map);

        /*
        1.simpleAdapter使用的數據源必須繼承Map接口
        2.from參數的意思是指向數據源Map中的鍵
        3.to 參數的意思是為布局中的控件Id賦值
         */
        SimpleAdapter sa=new SimpleAdapter(this,list,
                R.layout.lll,
                new String[]{"img","name"},
                new int[]{R.id.iv,R.id.tv});
       lv.setAdapter(sa);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(),"點擊行:"+id,Toast.LENGTH_SHORT).show();
            }
        });
    }
}






圖片+漢子布局

 

 



    
    
\

ListView優化----Holder
\

 

ListView多布局

\

ListView優化-----分段顯示

\

\

新聞ListView---------------------重點重點重點

\

 

package com.example.jreduch7292.entity;

/**
 * Created by 沖天之峰 on 2016/8/3.
 */
public class News {

    private String title;
    private String pubDate;
    private String newId;
    private int img;
    private String from;

    public News(String title, String pubDate, String newId, int img, String from) {
        this.title = title;
        this.pubDate = pubDate;
        this.newId = newId;
        this.img = img;
        this.from = from;
    }

    public String getTitle() {
        return title;
    }

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

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public String getNewId() {
        return newId;
    }

    public void setNewId(String newId) {
        this.newId = newId;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }
}
package com.example.jreduch7292.entity;

/**
 * Created by 沖天之峰 on 2016/8/3.
 */
public class News {

    private String title;
    private String pubDate;
    private String newId;
    private int img;
    private String from;

    public News(String title, String pubDate, String newId, int img, String from) {
        this.title = title;
        this.pubDate = pubDate;
        this.newId = newId;
        this.img = img;
        this.from = from;
    }

    public String getTitle() {
        return title;
    }

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

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public String getNewId() {
        return newId;
    }

    public void setNewId(String newId) {
        this.newId = newId;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }
}
布局程序

 

 




    
    
    
package com.example.jreduch7292.listdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.jreduch7292.R;
import com.example.jreduch7292.adapter.MyListAdapter;
import com.example.jreduch7292.entity.News;

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

public class List3Activity extends AppCompatActivity {
private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list3);
        lv=(ListView)findViewById(R.id.lv);
        List list=new ArrayList();
        News ns=new News("新聞1","剛剛","n001",R.mipmap.a,"人民網");
        list.add(ns);
        ns=new News("新聞2","1小時前","n002",R.mipmap.b,"新浪網");
        list.add(ns);
        ns=new News("新聞3","剛剛","n003",R.mipmap.c,"搜狐網");
        list.add(ns);
        ns=new News("新聞4","剛剛","n004",R.mipmap.d,"谷歌網");
        list.add(ns);
        ns=new News("新聞5","1小時前","n005",R.mipmap.e,"新浪網");
        list.add(ns);
        ns=new News("新聞6","3小時前","n006",R.mipmap.f,"百度網");
        list.add(ns);
        ns=new News("新聞7","剛剛","n007",R.mipmap.g,"新浪網");
        list.add(ns);
        ns=new News("新聞8","1小時前","n008",R.mipmap.h,"新浪網");
        list.add(ns);
        ns=new News("新聞9","剛剛","n009",R.mipmap.zyf,"天涯網");
        list.add(ns);
        ns=new News("新聞10","5小時前","n0010",R.mipmap.zyfzyf,"新浪網");
        list.add(ns);
        ns=new News("新聞1","剛剛","n001",R.mipmap.a,"人民網");
        list.add(ns);
        ns=new News("新聞2","1小時前","n002",R.mipmap.b,"新浪網");
        list.add(ns);
        ns=new News("新聞3","剛剛","n003",R.mipmap.c,"搜狐網");
        list.add(ns);

        lv.setAdapter(new MyListAdapter(this,list));
    }
}








\


新聞ListView+點擊監聽

 

\

1.News2代碼

 

package com.example.jreduch7292.entity;

/**
 * Created by 沖天之峰 on 2016/8/3.
 */
public class News2 {

    private String title;
    private String pubDate;
    private String newId;
    private int img1;
    private int img2;
    private int img3;
    private String from;
    private String pinglun;
    private String jian;
    private String x;
   // private int goodCount;

    public News2( String jian, String x, String pinglun, String from, int img3, int img2, int img1, String newId, String pubDate, String title) {

        this.jian = jian;
        this.x = x;
        this.pinglun = pinglun;
        this.from = from;
        this.img3 = img3;
        this.img2 = img2;
        this.img1 = img1;
        this.newId = newId;
        this.pubDate = pubDate;
        this.title = title;
    }


    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    public String getJian() {
        return jian;
    }

    public void setJian(String jian) {
        this.jian = jian;
    }

    public String getPinglun() {
        return pinglun;
    }

    public void setPinglun(String pinglun) {
        this.pinglun = pinglun;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getImg3() {
        return img3;
    }

    public void setImg3(int img3) {
        this.img3 = img3;
    }

    public int getImg2() {
        return img2;
    }

    public void setImg2(int img2) {
        this.img2 = img2;
    }

    public int getImg1() {
        return img1;
    }

    public void setImg1(int img1) {
        this.img1 = img1;
    }

    public String getNewId() {
        return newId;
    }

    public void setNewId(String newId) {
        this.newId = newId;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
2.MyList2Adapter代碼

 

 

package com.example.jreduch7292.adapter;

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;
import android.widget.Toast;

import com.example.jreduch7292.R;
import com.example.jreduch7292.entity.News2;

import java.util.List;

/**
 * Created by 沖天之峰 on 2016/8/3.
 */
public class MyList2Adapter extends BaseAdapter {
    private List myData;

    private Context context;
    public  MyList2Adapter( Context context,List myData){
        this.context=context;
        this.myData=myData;
    }

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

    @Override
    public Object getItem(int position) {
        return myData.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
   final     ViewHolder vh;
     if(convertView==null){
         vh=new ViewHolder();
         convertView=LayoutInflater.from(context).inflate(R.layout.list2_base,null);
         vh.title=(TextView)convertView.findViewById(R.id.title);
        vh.pubDate=(TextView)convertView.findViewById(R.id.time);
        vh.from=(TextView) convertView.findViewById(R.id.from);
       vh.pinglun=(TextView) convertView.findViewById(R.id.pinglun);
        vh.jian=(TextView) convertView.findViewById(R.id.jian);
        vh.x=(TextView) convertView.findViewById(R.id.x);
        vh.img1=(ImageView)convertView.findViewById(R.id.img1);
        vh.img2=(ImageView)convertView.findViewById(R.id.img2);
        vh.img3=(ImageView)convertView.findViewById(R.id.img3);

         convertView.setTag(vh);
     }else{

         vh= (ViewHolder)convertView.getTag();
     }
        vh.img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"點擊了圖片"+position,Toast.LENGTH_SHORT).show();
            }
        });

        final News2 ns=myData.get(position);
        vh.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"點擊了評論+1",Toast.LENGTH_SHORT).show();
               int count=Integer.parseInt(vh.pinglun.getText().toString())+1;
                vh.pinglun.setText(count+"");
               // vh.pinglun.setTextColor(0xffcc0000);
                ns.setPinglun(String.valueOf(count));
            }
        });


//       News2 ns= myData.get(position);
        vh.title.setText(ns.getTitle());
        vh. img1.setImageResource(ns.getImg1());
        vh. img2.setImageResource(ns.getImg2());
        vh. img3.setImageResource(ns.getImg3());
        vh. jian.setText(ns.getJian());
        vh. from.setText(ns.getFrom());
        vh. pinglun.setText(ns.getPinglun());
        vh. pubDate.setText(ns.getPubDate());
        vh. x.setText(ns.getX());
       // vh.goodCount.setText(ns.getGoodCount());


        return convertView;
    }




//        View view= LayoutInflater.from(context).inflate(R.layout.list2_base,null);
//        TextView title=(TextView) view.findViewById(R.id.title);
//        TextView pubDate=(TextView) view.findViewById(R.id.time);
//        TextView  from=(TextView) view.findViewById(R.id.from);
//        TextView  pinglun=(TextView) view.findViewById(R.id.pinglun);
//        TextView  jian=(TextView) view.findViewById(R.id.jian);
//        TextView x=(TextView) view.findViewById(R.id.x);
//        ImageView img1=(ImageView)view.findViewById(R.id.img1);
//        ImageView img2=(ImageView)view.findViewById(R.id.img2);
//        ImageView img3=(ImageView)view.findViewById(R.id.img3);
//
//        News2 news=myData.get(position);
//        title.setText(news.getTitle());
//        img1.setImageResource(news.getImg1());
//        img2.setImageResource(news.getImg2());
//        img3.setImageResource(news.getImg3());
//        jian.setText(news.getJian());
//        from.setText(news.getFrom());
//        pinglun.setText(news.getPinglun());
//        pubDate.setText(news.getPubDate());
//        x.setText(news.getX());


    public class ViewHolder{
        TextView title;
        ImageView img1;
        TextView pubDate;
        TextView  from;
        TextView  pinglun;
        TextView  jian;
        TextView x;
        ImageView img2;
        ImageView img3;
     //   TextView goodCount;
    }
}

 

3(R.layout.list2_base,null);布局

 



    
    
        
        
        
    
    
        
        


        
        
        
    



 

4.List2Activity代碼

package com.example.jreduch7292.listdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import com.example.jreduch7292.R; import com.example.jreduch7292.adapter.MyList2Adapter; import com.example.jreduch7292.entity.News2; import java.util.ArrayList; import java.util.List; public class List2Activity extends AppCompatActivity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list2); lv=(ListView)findViewById(R.id.lv); List list=new ArrayList(); News2 ns=new News2("薦","X","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,"n001","剛剛","新聞1"); list.add(ns); ns=new News2("薦","X","21323","人民網",R.mipmap.d,R.mipmap.e,R.mipmap.f,"n001","剛剛","新聞2"); list.add(ns); ns=new News2("薦","X","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,"n001","剛剛","新聞3"); list.add(ns); ns=new News2("薦","X","21323","人民網",R.mipmap.d,R.mipmap.e,R.mipmap.f,"n001","剛剛","新聞4"); list.add(ns); ns=new News2("薦","X","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,"n001","剛剛","新聞5"); list.add(ns); ns=new News2("薦","X","21323","人民網",R.mipmap.d,R.mipmap.e,R.mipmap.f,"n001","剛剛","新聞6"); list.add(ns); lv.setAdapter(new MyList2Adapter(this,list)); } } 

 

5.ListView布局

 



    

\

 

\

\

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved