Single

AIDE Android App基础教程_第十六课答案-Custom List 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 listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
}

我们在XML布局文件中添加“TextView”来自定义条目布局,属性分别为“android:layout_width=”wrap_content””、“android:layout_height=”wrap_content””、“android:padding=”10dp””和android:textSize=”25sp”

<?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:gravity="center_vertical"
	android:orientation="horizontal">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="25sp"
		android:padding="10dp"/>

</LinearLayout>

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

<?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:gravity="center_vertical"
	android:orientation="horizontal">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="25sp"
		android:padding="10dp"
		android:id="@+id/entryTextView1"/>

</LinearLayout>

现在在“TextView”之前添加“ImageView”,属性分别为“android:layout_width=”35dp””、“android:layout_height=”35dp””、android:layout_marginLeft=”10dp”

<?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:gravity="center_vertical"
	android:orientation="horizontal">

	<ImageView
		android:src="@android:drawable/ic_delete"
		android:layout_width="35dp"
		android:layout_height="35dp"
		android:layout_marginLeft="10dp"/>

	<TextView
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:id="@+id/entryTextView1"
		android:padding="10dp"
		android:textSize="25sp"/>

</LinearLayout>

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

<?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:gravity="center_vertical"
	android:orientation="horizontal">

	<ImageView
		android:src="@android:drawable/ic_delete"
		android:layout_width="35dp"
		android:layout_height="35dp"
		android:layout_marginLeft="10dp"
		android:id="@+id/entryImageView1"/>

	<TextView
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:id="@+id/entryTextView1"
		android:padding="10dp"
		android:textSize="25sp"/>

</LinearLayout>

使用条目布局,添加自定义列表适配器

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}
		
		
    }
}

我们为列表设置适配器并且以“companies”作为数据

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}
		
		
    }
}

MyAdapter”类覆写了方法“public View getView(int position, View convertView, ViewGroup parent)”,该方法负责创建条目视图,提示:只要在红色箭头处输入“getView”然后使用代码提示功能来完成

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return super.getView(position, convertView, parent);
        }
		
		
    }
}

在“getView”方法中,我们填入布局文件“entry.xml”作为条目布局

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.entry, parent, false);
			
			String text = getItem(position);
	
			
			
			return view;
		}
    }
}

我们使用“getItem()”获得条目的文本,文字是以数组作为数据传递给适配器

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.entry, parent, false);
			
			String text = getItem(position);
	
			
			
			return view;
		}
    }
}

使用代码“TextView textView = (TextView) view.findViewById(R.id.entryTextView1);”获得条目布局中的“TextView

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.entry, parent, false);
			
			String text = getItem(position);
	
			TextView textView=(TextView)view.findViewById(R.id.entryTextView1);
			
			return view;
		}
    }
}

设置“TextView”的文本,使用代码“textView.setText(text);

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.entry, parent, false);
			
			String text = getItem(position);
	
			TextView textView=(TextView)view.findViewById(R.id.entryTextView1);
            textView.setText(text);
			
			return view;
		}
    }
}

使用代码“ImageView imageView = (ImageView) view.findViewById(R.id.entryImageView1);”获取条目视图中“ImageView”的ID

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.entry, parent, false);
			
			String text = getItem(position);
	
			TextView textView = (TextView) view.findViewById(R.id.entryTextView1);
			textView.setText(text);
	
			ImageView imageView=(ImageView)view.findViewById(R.id.entryImageView1);
			
			return view;
		}
    }
}

使用代码“imageView.setImageResource(android.R.drawable.ic_menu_info_details);”设置“ImageView”的文本,您也可以用系统的图片“android.R.drawable.ic_menu_info_details”替换为您自己的图片

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.entry, parent, false);
			
			String text = getItem(position);
	
			TextView textView = (TextView) view.findViewById(R.id.entryTextView1);
			textView.setText(text);
	
			ImageView imageView=(ImageView)view.findViewById(R.id.entryImageView1);
            imageView.setImageResource(android.R.drawable.ic_menu_info_details);
			
			return view;
		}
    }
}

添加代码“if (“Google”.equals(text)) imageView.setImageResource(android.R.drawable.ic_menu_gallery);”为“Google”条目来设置不同的图标

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.graphics.*;

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 MyAdapter(this, companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(adapter);
        
    }
    
    class MyAdapter extends ArrayAdapter<String>
    {
		public MyAdapter(Context context, String[] values) 
		{
			super(context, R.layout.entry, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.entry, parent, false);
			
			String text = getItem(position);
	
			TextView textView = (TextView) view.findViewById(R.id.entryTextView1);
			textView.setText(text);
	
			ImageView imageView = (ImageView) view.findViewById(R.id.entryImageView1);
			imageView.setImageResource(android.R.drawable.ic_menu_info_details);
			
			if("Google".equals(text))
                imageView.setImageResource(android.R.drawable.ic_menu_gallery);
			
			return view;
		}
    }
}

欢迎加入AIDE教程网官方交流群:961607042

暂无评论

发表评论