ArrayAdapter adapter usage, simulating the QQ message sending interface ., Arrayadapter Adapter

Source: Internet
Author: User

ArrayAdapter adapter usage, simulating the QQ message sending interface ., Arrayadapter Adapter

1 import java. util. arrayList; 2 3 import android. app. activity; 4 import android. content. context; 5 import android. OS. bundle; 6 import android. view. layoutInflater; 7 import android. view. view; 8 import android. view. view. onClickListener; 9 import android. view. viewGroup; 10 import android. widget. arrayAdapter; 11 import android. widget. button; 12 import android. widget. editText; 13 import android. widget. listView; 14 import android. widget. textView; 15 16 public class MainActivity extends Activity {17 EditText et; 18 Button btn; 19 ListView lv; 20 MyAdapter mMyAdapter; 21 ArrayList <String> date; 22 23 @ Override24 protected void onCreate (Bundle savedInstanceState) {25 super. onCreate (savedInstanceState); 26 setContentView (R. layout. activity_main); 27 28 date = new ArrayList <String> (); 29 et = (EditText) findViewById (R. id. et1); 30 btn = (Button) findViewById (R. id. btn1); 31 lv = (ListView) findViewById (R. id. lv); 32 33 mMyAdapter = new MyAdapter (this, R. layout. list); 34 // set the ListView adapter (ArrayAdapter adapter usage) 35 lv. setAdapter (mMyAdapter); 36 37 btn. setOnClickListener (new OnClickListener () {38 39 @ Override40 public void onClick (View v) {41 sendmessage (); 42} 43}); 44} 45 46 public void sendmessage () {47 date. add (et. getText (). toString (); 48 mMyAdapter. notifyDataSetChanged (); 49 // The input box of the sent setting is null. 50 et. setText (null); 51 // scroll the ListView to the bottom 52 lv after the message is sent. setSelection (ListView. FOCUS_DOWN); 53} 54 55 public class MyAdapter extends ArrayAdapter {56 private Context context; 57 private int resource; 58 59 public MyAdapter (Context context, int resource) {60 super (context, resource); 61 this. resource = resource; 62 this. context = context; 63} 64 65 @ Override66 public int getCount () {67 68 return date. size (); 69} 70 71 @ Override72 public View getView (int position, View convertView, ViewGroup parent) {73 74 LayoutInflater flater = LayoutInflater. from (context); 75 // You can reuse view76 if (convertView = null) {77 convertView = flater. inflate (resource, null); 78} 79 TextView TV = (TextView) convertView. findViewById (R. id. qqtxt); 80 TV. setText (date. get (position); 81 82 return convertView; 83} 84 85} 86}

Xml file:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:id="@+id/RelativeLayout1" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" > 6  7     <ImageView 8         android:id="@+id/ImageView1" 9         android:layout_width="50dp"10         android:layout_height="50dp"11         android:layout_alignParentRight="true"12         android:layout_centerVertical="true"13         android:background="@drawable/meimei" />14 15     <TextView16         android:id="@+id/qqtxt"17         android:layout_width="wrap_content"18         android:layout_height="wrap_content"19         android:layout_centerVertical="true"20         android:layout_toLeftOf="@+id/ImageView1"21         android:background="#ff5252"22         android:text="1233"23         android:textSize="20sp" />24 25 </RelativeLayout>
1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 tools: context = "com. lixu. qqduihua. mainActivity "> 6 7 <ListView 8 android: id =" @ + id/lv "9 android: layout_width =" match_parent "10 android: layout_height =" wrap_content "11 android: layout_above = "@ + id/commentLinearLayout" 12 android: layout_alignParentTop = "true" 13 android: divider = "@ android: color/transparent" 14 android: dividerHeight = "15dip" 15 android: scrollbars = "none"/> 16 17 <LinearLayout18 android: id = "@ + id/commentLinearLayout" 19 android: layout_width = "match_parent" 20 android: layout_height = "wrap_content" 21 android: layout_alignParentBottom = "true" 22 android: background = "# e0e0e0" 23 android: orientation = "horizontal"> 24 25 <EditText26 android: id = "@ + id/et1" 27 android: layout_width = "wrap_content" 28 android: layout_height = "wrap_content" 29 android: layout_weight = "2" 30 android: EMS = "10" 31 android: hint = "Enter content"> 32 33 <requestFocus/> 34 </EditText> 35 36 <Button37 android: id = "@ + id/btn1" 38 android: layout_width = "wrap_content" 39 android: layout_height = "wrap_content" 40 android: background = "@ drawable/round" 41 android: text = "send"/> 42 </LinearLayout> 43 44 </RelativeLayout>

Running effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.