这是我的适配器
private class CustomAdapter extends BaseAdapter{
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text=new TextView(MainActivity.this);
text.setText("SAmple text");
Button button =(Button)new Button(MainActivity.this);
button.setText("Click me");
EditText edit=new EditText(MainActivity.this);
LinearLayout layout=new LinearLayout(getApplicationContext());
ImageView image=new ImageView(MainActivity.this);
CheckBox check=new CheckBox(MainActivity.this);
ToggleButton toggle=new ToggleButton(getApplicationContext());
check.setChecked(true);
layout.addView(check);
layout.addView(toggle);
if(position==0){
convertView=text;
}
else if( position==1){
convertView= button;
}
else if(position==2){
convertView= edit;
}
else if (position==3) {
RelativeLayout rel=new View1(MainActivity.this);
convertView=rel;
}
else{
convertView=layout;
}
return convertView;
}
这是我的自定义视图
package org.example.demo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;
public class View1 extends RelativeLayout{
public View1(Context context) {
super(context);
// TODO Auto-generated constructor stub
setView(context);
}
public void setView(Context context){
CheckBox check=new CheckBox(context);
ToggleButton toggle=new ToggleButton(context);
addView(check);
addView(toggle);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
}
}
这是我得到的
问题是我无法获得在适配器之外创建的视图。在适配器内部创建视图或视图组或将其引用到外部不是一回事吗?还是我错过了一些覆盖布局方法的东西?