Android interface component ---- List View, Android component ---- View

Source: Internet
Author: User

Android interface component ---- List View, Android component ---- View

The ListView list view is the most common component in Android. It displays the list items to be displayed in a vertical list, such as our system settings, function list, and pull-down refresh of the news client.

Specifically, ListView can be created not only through components, but also through the developer's own inheritance of ListActivity.

 

1. Using the ListView component

As a component, pay attention to the following basic attributes of ListView:

Android: divider sets Separators for the List View, which can be separated by colors or drawable resources.

Android: dividerHeight: sets the height of the separator bar.

Android: entries specifies the ListView list item through the resource Array

Android: footerDividersEnable: whether to set a separator before setting foot view; if the current attribute is true, you can set footer view by addFooterView ().

Android: headerDividersEnable: whether to set a separator before setting the head view. If the current attribute is true, you can set the head view using addFooterView ().

Actual Operation:

1. Add a list view to the layout file and configure the display content through the xml resource file.

// The ListView added in the layout. The resource file data is not specified.

<ListView
Android: id = "@ + id/listView1"
Android: divider = "@ drawable/divide"
Android: dividerHeight = "3dp"
Android: footerDividersEnabled = "true"
Android: headerDividersEnabled = "true"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
</ListView>
// Data specified in the resource file

<String-array name = "data"> <item> first </item> <item> second </item> <item> third </item> </string-array>

Running effect:

2. Set the displayed content through the adapter

What is important about the list view is the content to be displayed: like the content to be displayed, when the layout xml file does not specify the content to be displayed, you can set a sequence to set a column chart to view the content of the table.

The use of a column chart is divided into two steps: 1> Create an adapter 2> associate an adapter with a column chart table component

Create an adapter:You can create an adapter through a resource file or a string array. Different from the drop-down box, you need to specify the format of the column chart as shown in the table (whether to include a check box or a single sequence)

Resource file creation:

        ArrayAdapter<CharSequence> a = ArrayAdapter.createFromResource(this, R.array.data,                android.R.layout.simple_list_item_checked);

Create from a string array:

        String[] str = new String[]{"first","second","third"};                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,str);

Bind an adapter:

        ListView v = (ListView)findViewById(R.id.listView1);        v.setAdapter(adapter);

Note: The running effect has been set to display the content

3. Implement listening

        v.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> v, View arg1, int pos,                    long id) {                                String result = v.getItemAtPosition(pos).toString();                Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();                            }        });

 

2. inherit the ListActivity implementation

If the program only needs to display a list window, it can inherit the ListActivity implementation directly. After the ListActivity is inherited, The onCreate () method does not need the setContentView () method to set the display container. The ListActivity has already been set to display. In this case, you only need to configure the adapter for ListActivity.

You need to inherit the ListActivity, and then add the settings. Key code:

public class MainActivity extends ListActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        ArrayAdapter<CharSequence> a = ArrayAdapter.createFromResource(this, R.array.data,                android.R.layout.simple_list_item_checked);                setListAdapter(a);            }

The ListActivity event listening is different from the Activity listening. The ListActivity has implemented the listening event, and the developer can overwrite onListItemClick.

    @Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {                super.onListItemClick(l, v, position, id);                String result = l.getItemAtPosition(position).toString();        Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();    }

Summary: similar to the drop-down list view, you can configure the content in xml or use an adapter for adaptation. The implementation of ListView effects can also inherit from ListActivity. ListActivity has implemented the Activity method and does not need to set setContentView (). The event listening overwrites the original onListItemClick ()

 

The level is limited. Please leave a message!

 

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.