欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中使用ListView模拟微信好友功能

程序员文章站 2023-12-16 11:31:22
效果图: 分析: 1、创建listview 2、创建数据 3、创建适配器   将数据放到呈现数据的容器里面。   将这个容器(带数据)连接适配器。  ...

效果图:

Android中使用ListView模拟微信好友功能Android中使用ListView模拟微信好友功能

分析:

Android中使用ListView模拟微信好友功能

1、创建listview

2、创建数据

3、创建适配器

  将数据放到呈现数据的容器里面。

  将这个容器(带数据)连接适配器。

    其实是直接在我们自己写的adapter的getview重载方法中返回连接的view。   

 view view=view.inflate(mcontext, com.example.wechatfriends.r.layout.item_friend, null);
    return view;

4、listview设置适配器

代码:

package fry;
import java.util.arraylist;
import java.util.list;
import com.example.wechatfriends.r;
import android.app.activity;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.adapterview.onitemselectedlistener;
import android.widget.arrayadapter;
import android.widget.listview;
import android.widget.relativelayout;
import android.widget.toast;
public class activity01 extends activity implements onitemselectedlistener,onitemclicklistener{
  private friendmodel friend;
  private listview listview;
  private list<friendmodel> list;
  private wechatlistadapter adapter;
  //存资源图片id
  private int[] imageid=new int[]{r.drawable.image1,r.drawable.image2,
      r.drawable.image3,r.drawable.image4,r.drawable.image5,r.drawable.image6,
      r.drawable.image7,r.drawable.image8,r.drawable.image9,r.drawable.image10,
      r.drawable.image11};
  //存昵称
  private string[] nickname=new string[]{"张三","吴京","战狼","神烦xp","木鱼"
      ,"水心","系大大","电影","血怒","创奇","讲故事"
  };
  @override
  protected void oncreate(bundle savedinstancestate) {
    // todo auto-generated method stub
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity01);
    init();
    setdata();
  }
  private void setdata() {
    //这里要是写成for(int i:imageid),那么i就是资源id,例如2130837505
    for(int i=0;i<imageid.length;i++){
      friendmodel friend1=new friendmodel();
      //system.out.println(i);
      friend1.setimagenum(imageid[i]);
      friend1.setnickname(nickname[i]);
      friend1.setsignature("我要做比海贼王还强大的人");
      list.add(friend1);
    }
    adapter=new wechatlistadapter(list, this);
    listview.setadapter(adapter);
  }
  private void init() {
    listview=(listview) findviewbyid(r.id.listview);
    listview.setonitemselectedlistener(this);
    listview.setonitemclicklistener(this);
    friend=new friendmodel();
    list=new arraylist<friendmodel>();
  }
  /*
   * callback method to be invoked when an item in this view has been selected. this callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.(non-javadoc)
   * @see android.widget.adapterview.onitemselectedlistener#onitemselected(android.widget.adapterview, android.view.view, int, long)
   */
  @override
  public void onitemselected(adapterview<?> parent, view view, int position,
      long id) {
  }
  @override
  public void onnothingselected(adapterview<?> parent) {
    // todo auto-generated method stub
  }
  @override
  public void onitemclick(adapterview<?> parent, view view, int position,
      long id) {
    friendmodel frienditem=(friendmodel) parent.getitematposition(position);
    string s=frienditem.getnickname();
    log.d("onitemclick","s");
    toast.maketext(this, s, toast.length_short).show();
  }
}
package fry;
import java.util.list;
import com.example.wechatfriends.r;
import android.content.context;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.imageview;
import android.widget.textview;
public class wechatlistadapter extends baseadapter{
  private list<friendmodel> mydata;
  private context mcontext;
  private imageview avator;
  private textview nickname1;
  private textview signature1;
  private friendmodel friend;
  public wechatlistadapter(list<friendmodel> data, context mcontext) {
    super();
    this.mydata = data;
    this.mcontext = mcontext;
  }
  //how many items are in the data set represented by this adapter.
  @override
  public int getcount() {
    // todo auto-generated method stub
    return this.mydata.size();
  }
  //get the data item associated with the specified position in the data set.
  @override
  public object getitem(int position) {
    // todo auto-generated method stub
    return this.mydata.get(position);
  }
  //get the row id associated with the specified position in the list.
  @override
  public long getitemid(int position) {
    // todo auto-generated method stub
    return position;
  }
  //get a view that displays the data at the specified position in the data set. 
  @override
  public view getview(int position, view convertview, viewgroup parent) {
    // todo auto-generated method stub
    view view=view.inflate(mcontext, com.example.wechatfriends.r.layout.item_friend, null);
    //system.out.println(position);
    friend=mydata.get(position);
    int imageid=friend.getimagenum();
    string nickname=friend.getnickname();
    string signature=friend.getsignature();
    avator=(imageview) view.findviewbyid(r.id.iv_avator);
    nickname1=(textview)view.findviewbyid(r.id.tv_nickname);
    signature1=(textview)view.findviewbyid(r.id.tv_signature);
    avator.setimageresource(imageid);
    nickname1.settext(nickname);
    signature1.settext(signature);
    return view;
  }
}

自己创建的适配器

package fry;
public class friendmodel {
  //头像的图片id
  private int imagenum;
  //昵称
  private string nickname;
  //个性签名
  private string signature;
  public int getimagenum() {
    return imagenum;
  }
  public void setimagenum(int imagenum) {
    this.imagenum = imagenum;
  }
  public string getnickname() {
    return this.nickname;
  }
  public void setnickname(string nickname) {
    this.nickname = nickname;
  }
  public string getsignature() {
    return signature;
  }
  public void setsignature(string signature) {
    this.signature = signature;
  }
}

列表中联系人数据的封装

<?xml version="1.0" encoding="utf-8"?>
<listview xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/listview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</listview>
listview
 listview
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
  <imageview 
    android:id="@+id/iv_avator"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:src="@drawable/image1"
    />
  <textview 
    android:id="@+id/tv_nickname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_torightof="@+id/iv_avator"
    android:layout_centervertical="true"
    android:layout_marginleft="20dp"
    android:text="张三"
    />
  <textview 
    android:id="@+id/tv_signature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centervertical="true"
    android:layout_alignparentright="true"
    android:text="我要做比海贼王更强大的男人"
    />
</relativelayout>

用于存放数据的容器

上一篇:

下一篇: