您的位置:首页 > 其它

AdapterView及Adapter分析

2014-01-14 15:29 239 查看
本文分析AdapterView及Adapter及其子类

1、继承结构

View<-ViewGroup<-AdapterView<-AbsListView<-ListView<-ExpandableListView

Adapter<-ListAdapter(SpinnerAdapter)<-BaseAdapter<-ArrayAdapter

可以看出AdapterView是ListView的基类,Adapter是BaseAdapter的基类

Adapter用来为AdapterView提供数据,同时也用来设置item view

2、实操讲解了ArrayAdapter及BaseAdapter的用法,顺便讲了TextWatcher和Filter的使用

TextWatcher用来监听TextView文本的变化,Filter用来过滤Adapter的数据

public class SomeTest extends Activity {
//2、准备数据集。不管什么数据类型,都会调用toString
Object[] data = {"tiger","lion","rabbit","rat","wolf","fox"};
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

//1、准备ListView
ListView listView = (ListView) getWindow().findViewById(R.id.listView);

//3、将数据集加载到ListView
//Context,TextView,TextView的id,ArrayList
final ArrayAdapter arrayAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, Arrays.asList(data));
listView.setAdapter(arrayAdapter);
//继承BaseAdapter,自定义列表项
//		listView.setAdapter(mListAdapter);
button = (Button) findViewById(R.id.btn_delete);
editText = (EditText) findViewById(R.id.edit);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
}
});
//此方法为TextView的方法,即所有TextView的子类都有这个功能
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//使用ArrayAdapter自带的Filter
arrayAdapter.getFilter().filter(s);
if(s.length()>0){
button.setVisibility(View.VISIBLE);
}else{
button.setVisibility(View.INVISIBLE);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

@Override
public void afterTextChanged(Editable s) {
}
});
}
private ListAdapter mListAdapter = new BaseAdapter() {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//参数parent就是ListView
//convertView The old view to reuse,if possible
if(convertView==null){
if(position%2==0){
Log.d("info", "TextView-"+position);
//调用AdapterView.addView(View, LayoutParams)会报UnsupportedOperationException
convertView = LayoutInflater.from(SomeTest.this).inflate(android.R.layout.simple_list_item_1, parent, false);
//					convertView = new TextView(SomeTest.this);
}else{
Log.d("info", "EditText-"+position);
convertView = new EditText(SomeTest.this);
convertView.setBackgroundColor(Color.GREEN);
}
}

if(position%2==0){
((TextView)convertView).setText(data[position].toString());
}else{
((EditText)convertView).setHint(data[position].toString());
}
return convertView;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public Object getItem(int position) {
return data[position];
}

@Override
public int getCount() {
return data.length;
}
public int getItemViewType(int position) {
return position%2;
};
public int getViewTypeCount() {
return 2;
};
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="X"/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>

3、Spinner控件的用法

1) 首先在布局文件中插入Spinner标签:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:prompt="@string/ten"
android:entries="@array/general_array"/>
其中spinnerMode可选出弹出菜单样式,分dialog和dropdown两种样式;

promtp用于设置对话框标题文字;

enties设置列表内容。

也可在类中设置,相应为

s.setPrompt("十大元帅");
adapter = ArrayAdapter.createFromResource(this, R.array.general_array, android.R.layout.simple_spinner_item);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, generals);
s.setAdapter(adapter);

2) 为Spinner设置监听

s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
int index = parent.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You have selected item "+generals[index],
<span style="white-space:pre">	</span>Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息