AIDE Android App基础教程_第十四课答案-ListView(列表视图)

第十四课,本课中您将学习使用列表视图 运行基本应用程序

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">

	
</LinearLayout>

添加“ListView”设置属性“android:layout_width=”match_parent””、“android:layout_height=”match_parent”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">

	<ListView
		android:layout_width="match_parent"
		android:layout_height="match_parent"/>

</LinearLayout>

设置属性“android:id”为“@+id/mainListView1”,分配ID给“ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">

	<ListView
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:id="@+id/mainListView1"/>

</LinearLayout>

在“ListView”上,我们已经添加了公司名称的数组

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        
    }
}

使用“ListAdapter”填充“ListView”、以“this”作为上下文的构造函数、“android.R.layout.simple list_item_1”作为条目布局、companies作为数据

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,companies);
        
    }
}

现在通过“findViewById”方法获得“ListView”对象并将其分配给一个变量

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,companies);
        ListView lv=findViewById(R.id.mainListView1);
    }
}

最后调用“ListView”对象的“setAdapter()”方法,并传递“adapter”参数

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,companies);
        ListView lv=findViewById(R.id.mainListView1);
        lv.setAdapter(adapter);
    }
}

首先,我们调用“setOnItemClickListener()”方法为“ListView”设定一个“OnItemClickListener

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, companies);
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> l, View v, int position, long id)
				{
					
				}
			});
    }
}

现在在“onItemClick”方法里调用“l.getItemAtPosition(position)”方法获取列表项文本,并将其分配给一个变量“String s

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, companies);
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> l, View v, int position, long id)
				{
					String s=(String) l.getItemAtPosition(position);
				}
			});
    }
}

最后显示提示文本“s

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        	"Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
        	
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, companies);
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> l, View v, int position, long id)
				{
					String s=(String) l.getItemAtPosition(position);
                    Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
				}
			});
    }
}

恭喜!您已经学会了将列表视图添加到您的应用中,在接下来的课程中,您将学习自定义列表视图

 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/1929.html

梦雪

专注于AIDE教程分享

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐