Use of Android listview

Source: Internet
Author: User
Tags listsource

1. Define a simple adapter format.

First, define a layout as listviewitem. xml. which contains three textviews. The student ID, name, and class.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/cuslistViewItem"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"         android:layout_marginLeft="10dp"        android:layout_marginBottom="10dp"/>    <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"        android:layout_marginLeft="10dp"         android:layout_marginBottom="10dp"/>    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"        android:layout_marginLeft="10dp"        android:layout_marginBottom="10dp" /></LinearLayout>

Put a listview control in activity_main.xml, which is simple and not explained.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/textView1"        android:layout_marginTop="50dp" >    </ListView></RelativeLayout>

 

Create a data source

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView lv = (ListView)this.findViewById(R.id.listView1);ArrayList<HashMap<String,String>> listSource = new ArrayList<HashMap<String,String>>();for(int num=1; num< 10; num++){HashMap<String,String> map = new HashMap<String, String>();map.put("num",String.valueOf(num));map.put("name", "stu"+num );map.put("className", "software 2");listSource.add(map);}SimpleAdapter mStuInfo = new SimpleAdapter(this,listSource,R.layout.listviewitem, new String[] {"num","name","className"}, new int[]{R.id.textView1, R.id.textView2, R.id.textView3});lv.setAdapter(mStuInfo);}}

Finally, bind the listview to the data source of the adapter. For example.

Of course, you can add the itemclick event.

 lv.setOnItemClickListener(new ItemClickListener());

Then implement itemclicklistener.

 class ItemClickListener implements OnItemClickListener { //arg2 is the position //arg3 is row number@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubHashMap<String, String> item=(HashMap<String, String>) arg0.getItemAtPosition(arg2); Toast.makeText(MainActivity.this, item.get("name"), Toast.LENGTH_LONG).show();}}

The usage of listview and gridview is the same here. You can replace the listview control with the gridview control.

 

II. The following uses a new adapter form. The student picture will be added and the layout will be reorganized. The effect is as follows:

The Code is as follows:

The redesigned listviewitem. xml.

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/cuslistViewItem"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginBottom="10dp" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"         android:layout_toRightOf="@+id/imageView1"        android:layout_marginLeft="10dp"        android:layout_marginBottom="10dp"/>    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"        android:layout_toRightOf="@+id/textView1"         android:layout_marginLeft="10dp"         android:layout_marginBottom="10dp"/>    <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView"        android:layout_toRightOf="@+id/imageView1"        android:layout_below="@+id/textView1"        android:layout_marginLeft="10dp"        android:layout_marginBottom="30dp" /></RelativeLayout>

 

Data Source again. The mylistviewadapter class is created to inherit the baseadapter.

Public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); listview Lv = (listview) This. findviewbyid (R. id. listview1); arraylist 

 

Related Article

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.