一:AutoCompleteTextView
package com.example.adapterdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
private String[] items={"Android1","Android2","Android3",
"Android4","Java1","Java2","Java3","Java4","Java5"};
private AutoCompleteTextView acTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
acTV=(AutoCompleteTextView) findViewById(R.id.autoTv);
//创建adapter
/**
* 1.上下文
* 2.布局,Android自带的布局
* 3.数据源(数组或集合)
* 用动态创建的数组或集合
*/
//从xml文件中获取已经定义好的数据源
// String[] course =getResources().getStringArray(R.array.course);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
//通过使用createFromResource方法从xml中获取数据源创建适配器
// ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
// R.array.course, android.R.layout.simple_list_item_1);
acTV.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<AutoCompleteTextView
android:id="@+id/autoTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="31dp"
android:hint="请输入关键词"
android:completionThreshold="1" >
</AutoCompleteTextView>
</RelativeLayout>
二:Spinner
package com.example.adapterdemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class SpinnerActivity extends Activity {
private Spinner spCity;
private TextView tv;
private String[] citys={"北京","上海","廣州","深圳","濟南"};
private ArrayList<String>list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
InitView();
//1.定義數據源
list=new ArrayList<String>();
list.add("北京");
list.add("上海");
list.add("广州");
list.add("深圳");
//2.ArrayAdapter實現數據源和spinner之間的橋樑
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list);
//3.设置spinner显示样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//4.将adapter添加到Spinner上
spCity.setAdapter(adapter);
spCity.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
String text = adapterView.getItemAtPosition(position).toString();
tv.setText(text);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private void InitView() {
spCity = (Spinner) findViewById(R.id.spCity);
tv=(TextView) findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.spinner, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SpinnerActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Spinner
android:id="@+id/spCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp" />
</RelativeLayout>
三:ListView
package com.example.adapterdemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListviewActivity extends Activity {
private ListView lvFriend;
private ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
lvFriend=(ListView) findViewById(R.id.lvFriend);
//1.定义数据源,如果获取数据源代码较复杂,一般写到一个方法中执行
list = getData();
//2.创建适配器,此时项是字符串,可以采用ArrayAdapter
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
//3.将adapter添加到listview上
lvFriend.setAdapter(adapter);
//4.为listView添加监听
lvFriend.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
String name = adapterView.getItemAtPosition(position).toString();
Toast.makeText(ListviewActivity.this, name, Toast.LENGTH_SHORT).show();
}
});
}
private ArrayList<String> getData() {
list=new ArrayList<String>();
list.add("张三");
list.add("李四");
list.add("王五");
list.add("贾六");
return list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.listview, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ListviewActivity" >
<ListView
android:id="@+id/lvFriend"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</RelativeLayout>