13. Android-based ListView Learning & #183;, 13 androidlistview

Source: Internet
Author: User

13. Android-based ListView learning ·, 13 androidlistview
1. Add the ListView (1) XML layout file to the XML file

<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" >    <ListView         android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:entries="@array/names"        android:divider="#aa0000"        android:dividerHeight="20dp"        android:drawSelectorOnTop="false"        android:scrollbars="horizontal|vertical"        android:fastScrollEnabled="true"        android:listSelector="#00ffff"        >      </ListView>  </RelativeLayout>
(2) string. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> ListViewDemo </string> <string name = "action_settings"> Settings </string> <string name = "hello_world"> Hello world! </String> <string-array name = "names"> <item> plumber </item> <item> fixed </item> <item> ELE. Me high </item> <item> On the contrary, </item> <item> change </item> <item> recommended </item> <item> inside the compartment </item> <item> identity </item> <item> Participation award </item> <item> for example, eye hunting </item> <item> stylized </item> <item> rice cooker </item> <item> it's faster </item> <item> wife's </item> <item> wrong person </item> </string-array> </resources>
(3) MainActivity
package com.example.listviewdemo;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnItemClickListener{private ListView lv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv = (ListView) findViewById(R.id.listView);lv.setOnItemClickListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubSystem.out.println("Parent = " + arg0.getClass());System.out.println("View = " + arg1);TextView tv = (TextView)arg1;Toast.makeText(this,tv.getText(), Toast.LENGTH_SHORT).show();System.out.println("Position = " + arg2);System.out.println("ID = " + arg3);}}
2. Use ListActivity and ArrayAdapter because ListActivity itself is an Activity, only when the page has only one space and the Space ID is certain.
Import android. app. listActivity; import android. OS. bundle; import android. view. view; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. textView; import android. widget. toast; public class MainActivity2 extends ListActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); String [] names = {"Zhang San", "Li Si", "Zhao Wu", "Wang Liu", "Zhou Qi", "Yang ba ", "document" ,}; ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, names); setListAdapter (adapter) ;}@ Overrideprotected void onListItemClick (ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper. onListItemClick (l, v, position, id); TextView TV = (TextView) v; Toast. makeText (this, TV. getText (), Toast. LENGTH_SHORT ). show ();}}
3. Create a ListView using the Adapter to achieve radio selection and check (1) XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <ListView         android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        >    </ListView></LinearLayout>
(2) Activity
Package com. example. listviewdemo; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity3 extends Activity implements OnItemClickListener {private ListView lv; @ Overrideprotected void onCreate (Bundle SavedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. main3); lv = (ListView) findViewById (R. id. listView1); String [] citys = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Dongguang", "China "}; // radio mode // ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_single_choice, citys); // lv. setChoiceMode (ListView. CHOICE_MODE_SINGLE); // multiple choice mode ArrayA Dapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_multiple_choice, citys); lv. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE); lv. setOnItemClickListener (this); // registers the event lv. setAdapter (adapter) ;}@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubSystem. out. println ("View =" + arg1 );}}









Hierarchical display of android listview and textview

Just get a translucent background.
When the layout is complete, listview is filled with windows, while textview is aligned at the bottom of the window.

In the process of android development and learning, a small problem occurs.

You add a View or a group of views to each Item. You can set the background for them and set the desired background image, set the background in the listview to the background of the entire listview instead of an item. For example, if you add a textView to each item, you only need to set textvie. setBackground.

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.