AndroidListView⽰例教程
We will learn how to create a simple Android ListView and launch a new activity on selecting a single list item.
我们将学习如何创建⼀个简单的Android ListView并在选择单个列表项时启动新的活动。
什么是Android ListView? (What is Android ListView?)
Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database. It’s one of the basic and most used UI components of android. The most common usages include displaying data in the form of a vertical scrolling list.
Android ListView是将多个项⽬分组并在垂直滚动列表中显⽰的视图。 列表项是使⽤适配器从列表或数据库等源中⾃动提取内容的适配器⾃动插⼊列表中的。 它是android的基本和最常⽤的UI组件之⼀。 最常见的⽤法包括以垂直滚动列表的形式显⽰数据。
使⽤适配器 (Using an Adapter)
An adapter actually bridges between UI components and the data source that fill data into UI Component. Adapter holds the data and send the data to adapter view, the view can take the data from adapter view and shows the data on different views like as spinner, list view, grid view etc. The adapter pulls the items out of a data source, an array for example, and then converts each item into a view which it then inserts into the ListView.
适配器实际上是UI组件和将数据填充到UI组件中的数据源之间的桥梁。 适配器保存数据并将数据发送到适配器视图,该视图可以从适配器视图获取数据,并在不同的视图(例如微调器,列表视图,⽹格视图等)上显⽰数据。适配器将项⽬从数据源中拉出,例如⼀个数组,然后将每个项⽬转换为⼀个视图,然后将其插⼊到ListView中。
The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry. The common adapters are ArrayAdapter, BaseAdapter, CursorAdapter, SimpleCursorAdapter, SpinnerAdapter and WrapperListAdapter.
ListView和GridView是AdapterView⼦类,可以通过将它们绑定到Adapter来进⾏填充,该Adapter从外部源检索数据并创建⼀个表⽰每个数据条⽬的View。 常见的适配器是ArrayAdapter,BaseAdapter,CursorAdapter,SimpleCursorAdapter,SpinnerAdapter和WrapperListAdapter 。
处理Android ListView点击 (Handling Android ListView Clicks)
The onListItemClick() method is used to process the clicks on android ListView item. This method receives 4 parameters:
onListItemClick()⽅法⽤于处理对Android ListView项⽬的点击。 此⽅法接收4个参数:
1. ListView : The ListView containing the item views
ListView :包含项⽬视图的ListView
2. View : The specific item view that was selected
视图 :选择的特定项⽬视图
3. Position : The position of the selected item in the array. Remember that the array is zero indexed, so the first item in
the array is at position 0
位置 :所选项⽬在数组中的位置。 请记住,数组的索引为零,因此数组的第⼀项位于位置0
4. Id : The id of the selected item. Not of importance for our tutorial but is important when using data retrieved from a
database as you can use the id (which is the id of the row containing the item in the database) to retrieve the item from the database
ID :所选项⽬的ID。 对于我们的教程⽽⾔并不重要,但是在使⽤从数据库中检索到的数据时⾮常重要,因为您可以使⽤ID(数据库中包含该项⽬的⾏的ID)从数据库中检索项⽬
Android ListView⽰例项⽬结构 (Android ListView Example Project Structure)
Let’s begin with defining the string resources file to store all list item labels. So we create an XML file under values folder and name it l and paste the following code.
让我们开始定义字符串资源⽂件以存储所有列表项标签。 因此,我们在values⽂件夹下创建⼀个XML⽂件,并将其命名为l并粘贴以下代码。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="teams">
<item>India</item>
<item>South Africa</item>
<item>Australia</item>
<item>England</item>
<item>New Zealand</item>
<item>Sri Lanka</item>
<item>Pakistan</item>
<item>West Indies</item>
<item>Bangladesh</item>
<item>Ireland</item>
</string-array>
</resources>
Each list view item will be represented by an xml layout,so lets define the xml layout comprising of a single textview as follows:
每个列表视图项将由⼀个xml布局表⽰,因此让我们定义⼀个包含单个textview的xml布局,如下所⽰:
l
l
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="schemas.android/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
Following snippet shows how to import the xml resources data and store them in data followed by binding them to the adapter:
以下⽚段显⽰了如何导⼊xml资源数据并将其存储在数据中,然后将它们绑定到适配器:
// storing string resources into Array
String[] teams = getResources().getStringArray(ams);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, view, teams));
In the following code we fetch the data value from the selected item and pass it as a bundle to the next activity using intents.
在下⾯的代码中,我们从选定的项⽬中获取数据值,然后将其作为数据包使⽤intent传递给下⼀个活动。
MainActivity.java
MainActivity.java
package journaldev.listview;
import android.app.ListActivity;
t.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
android学习教程Create(savedInstanceState);
// storing string resources into Array
String[] teams = getResources().getStringArray(ams);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, view, teams));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String team = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
// sending data to new activity
i.putExtra("team", team);
startActivity(i);
}
});
}
}
The SecondActivity class retrieves the text label from the list item selected and displays it in a textview as shown in the following snippet.
SecondActivity类从选定的列表项中检索⽂本标签,并将其显⽰在textview中,如以下代码⽚段所⽰。
SecondActivity.java
SecondActivity.java
package journaldev.listview;
import android.app.Activity;
t.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_second);
TextView txtProduct = (TextView) findViewById(am_label);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("team");
// displaying selected product name
txtProduct.setText(product);
}
}
Following small GIF depict the flow of the app:
以下⼩GIF描述了应⽤程序的流程:
That’s all for a quick android listview example. You should also learn about . You can download android list view project from below link.
这就是⼀个快速的Android listview⽰例的全部。 您还应该了解 。 您可以从下⾯的链接下载android list view项⽬ 。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论