In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains how to use ListActivity in Android. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "How to use ListActivity in Android"!
Let's start by looking at a few classes in the android.app package. Android ListActivity is widely used in platform examples. This class is actually an Activity class that contains a ListView component. In other words, if we add a ListView directly to an ordinary Activity, it can completely replace this Android ListActivity, but it is more convenient. Here's an example.
public class HelloTwoB extends ListActivity
... {
public void onCreate(Bundle icicle) ... {
super.onCreate(icicle);
setTheme(android.R.style.Theme_Dark);
setContentView(R.layout.mainb);
List
< String>items = fillArray();
ArrayAdapter
< String>adapter = new ArrayAdapter
< String>(this,R.layout.list_row,items);
this.setListAdapter(adapter);
}
private List
< String>fillArray()
... {
List
< String>items = new ArrayList
< String>();
items.add("Daylight");
items.add("Month Day");
items.add("");
items.add("");
items.add("");
items.add("");
items.add("Sunday");
return items;
}
@Override
protected void onListItemClick(ListView l,
View v, int position, long id)
... {
TextView txt = (TextView)this.findViewById(R.id.text);
txt.setText("あすは "+l.getSelectedItem().toString()+"です。");
}
}
It can be as simple as preparing a List object and constructing a list with the help of an Adapter. The overloaded onListItemClick method responds to the selection event by accessing the ListView instance with *** parameters to get information about the selected item. Here is a point to explain, that is, if it is simpler, in fact, even the setContentView can not be, Android will automatically help us construct a full-screen list. But in this case we need a TextView to display the selected items, so we need a layout.mainb to describe the list window.
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > < TextView id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> < ListView id="@id/android:list" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:drawSelectorOnTop="false" /> < /LinearLayout>In the Android ListActivity operation, we need to pay attention to the ID of the ListView, which is the system-defined android:list, not what we take casually, otherwise the system will say that it cannot find the listview it wants. Then, in addition to this listview, we add a TextView to display the selected items.
Let's talk about ArrayAdapter used here. The second parameter in its constructor is a resource ID. The API documentation of ArrayAdapter says that it requires a layout file containing TextView, which is used by the platform to display the style of each selected item. The value here is R.layout.list_row. Therefore, we also have a list_row.xml file to describe this layout, which is quite simple.
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > < TextView id="@+id/item" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"/> < TextView id="@+id/item2" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"/> < /LinearLayout>At this point, I believe that everyone has a deeper understanding of "how to use ListActivity in Android," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.