Android开发教程:使用ListView实现汽泡短信聊天


如这篇文章:

我们进行了SimpleAdapter适配器初次尝试,那么离实现我们最终想要的效果也不远啦,只要仿照chata的布局,再编写第二位聊天人(“路人甲”)的布局chatb——只要让他靠右显示就行~。

但是这样我们每次都要很麻烦的定义一遍SimpleAdapter,为了“偷懒”,我们直接来编写自己的Adapter,这样每次定义就方便多了。

先附上最终的代码:

  1. public void onCreate(Bundle savedInstanceState) {  
  2.             super.onCreate(savedInstanceState);  
  3.             setContentView(R.layout.main);  
  4.             chatlist = (ListView) findViewById(R.id.chatlist);  
  5.             list = new ArrayList<ChatEntity>();  
  6.             ChatEntity chat1=new ChatEntity("小魏","嗨~",R.layout.chata);  
  7.             list.add(chat1);  
  8.             ChatEntity chat2=new ChatEntity("路人甲","你好!",R.layout.chatb);  
  9.             list.add(chat2);  
  10.             ChatEntity chat3=new ChatEntity("小魏","我是小魏~",R.layout.chata);  
  11.             list.add(chat3);  
  12.               
  13.             chatlist.setAdapter(new ChatAdapter(TryChatPop2Activity.this,list));  
  14. }  

如上代码,在setAdapter时使用了自己的ChatAdapter,以下是类文件代码:

  1. public class ChatAdapter implements ListAdapter{  
  2.     private ArrayList<ChatEntity> list;  
  3.     private Context ctx;  
  4.   
  5.     public ChatAdapter(Context context ,ArrayList<ChatEntity> list) {  
  6.         ctx = context;  
  7.         this.list = list;  
  8.     }  
  9.       
  10.     public boolean areAllItemsEnabled() {  
  11.         return false;  
  12.     }  
  13.     public boolean isEnabled(int arg0) {  
  14.         return false;  
  15.     }  
  16.     public int getCount() {  
  17.         return list.size();  
  18.     }  
  19.     public Object getItem(int position) {  
  20.         return list.get(position);  
  21.     }  
  22.     public long getItemId(int position) {  
  23.         return position;  
  24.     }  
  25.     public int getItemViewType(int position) {  
  26.         return position;  
  27.     }  
  28.     public View getView(int position, View convertView, ViewGroup parent) {  
  29.         ChatEntity entity = list.get(position);  
  30.         int itemLayout = entity.getLayoutID();  
  31.           
  32.         LinearLayout layout = new LinearLayout(ctx);  
  33.         LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  34.         vi.inflate(itemLayout, layout,true);  
  35.           
  36.         TextView txvName = (TextView) layout.findViewById(R.id.txvName);  
  37.         txvName.setText(entity.getName());  
  38.           
  39.         TextView txvText = (TextView) layout.findViewById(R.id.txvInfo);  
  40.         txvText.setText(entity.getInfo());  
  41.         return layout;  
  42.     }  
  43.     public int getViewTypeCount() {  
  44.         return list.size();  
  45.     }  
  46.     public boolean hasStableIds() {  
  47.         return false;  
  48.     }  
  49.     public boolean isEmpty() {  
  50.         return false;  
  51.     }  
  52.     public void registerDataSetObserver(DataSetObserver observer) {  
  53.     }  
  54.     public void unregisterDataSetObserver(DataSetObserver observer) {  
  55.     }  
  56.   
  57. }  
  • 1
  • 2
  • 下一页

相关内容