1 :Android中Spinner下拉列表(运用ArrayAdapter和自界说Adapter完成) .
今日学习了Spinner组件,运用Spinner相当于从下拉列表中选择项目,下面演示一下Spinner的运用(别离运用ArrayAdapter和自界说Adapter完成)
(一):运用ArrayAdapter进行适配数据:
①:首要界说一个布局文件:
android:layout_width=fill_parent
android:layout_height=fill_parent
android:orientation=vertical >
android:id=@+id/spinner1
android:layout_width=match_parent
android:layout_height=wrap_content
/>
②:树立数据源,运用数组,这些数据将会在Spinner下来列表中进行显现:
③:接着在Activity中参加如下的代码(运用了体系界说的下拉列表的布局文件,当然也能够自界说)
// 初始化控件
mSpinner = (Spinner) findViewById(R.id.spinner1);
// 树立数据源
String[] mItems = getResources().getStringArray(R.array.spinnername);
// 树立Adapter而且绑定数据源
ArrayAdapter
//绑定 Adapter到控件
mSpinner.setAdapter(_Adapter);
下面是关于Spinner的点击事情(效果图如上图):
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView parent, View view,
int position, long id) {
String str=parent.getItemAtPosition(position).toString();
Toast.makeText(SpinnerActivity.this, 你点击的是:+str, 2000).show();
}
@Override
public void onNothingSelected(AdapterView parent) {
// TODO Auto-generated method stub
}
});
(二)运用自界说的Adapter(要点)
①:界说每一个Item的布局文件
android:layout_width=match_parent
android:layout_height=match_parent
android:orientation=horizontal >
android:id=@+id/textView1
android:layout_width=wrap_content
android:layout_height=wrap_content
android:drawableLeft=@drawable/ic_launcher
android:paddingRight=8dip
android:paddingTop=8dip
android:text=TextView
android:textSize=25sp />
android:id=@+id/textView2
android:layout_width=wrap_content
android:layout_height=wrap_content
android:paddingLeft=8dip
android:paddingTop=8dip
android:text=TextView
android:textSize=25sp />
②:树立Person类:
package com.jiangqq.csdn;
public class Person {
private String personName;
private String personAddress;
public Person(String personName, String personAddress) {
super();
this.personName = personName;
this.personAddress = personAddress;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public String getPersonAddress() {
return personAddress;
}
public void setPersonAddress(String personAddress) {
this.personAddress = personAddress;
}
}
③:创立MyAdapter承继与BaseAdapter,进行适配:
package com.jiangqq.csdn;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 自界说适配器类
* @author jiangqq
*
*/
public class MyAdapter extends BaseAdapter {
private List
private Context mContext;
public MyAdapter(Context pContext, List
this.mContext = pContext;
this.mList = pList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 下面是重要代码
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);
convertView=_LayoutInflater.inflate(R.layout.item, null);
if(convertView!=null)
{
TextView _TextView1=(TextView)convertView.findViewById(R.id.textView1);
TextView _TextView2=(TextView)convertView.findViewById(R.id.textView2);
_TextView1.setText(mList.get(position).getPersonName());
_TextView2.setText(mList.get(position).getPersonAddress());
}
return convertView;
}
}
④:在Activity中参加如下代码:
// 初始化控件
mSpinner = (Spinner) findViewById(R.id.spinner1);
// 树立数据源
List
persons.add(new Person(张三, 上海 ));
persons.add(new Person(李四, 上海 ));
persons.add(new Person(王五, 北京 ));
persons.add(new Person(赵六, 广州 ));
// 树立Adapter绑定数据源
MyAdapter _MyAdapter=new MyAdapter(this, persons);
//绑定Adapter
mSpinner.setAdapter(_MyAdapter);