将编程布局添加到我的适配器时出现问题
这是我的适配器
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
}
}
这是我得到的
问题是我无法获取在适配器外部创建的视图。在适配器内部创建视图或视图组或将其引用到外部不是同一件事吗?或者我错过了一些重写布局方法?
Here is my adapter
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;
}
And here is my custom view
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
}
}
Here is what I get
The problem is I can't get the view that I created outside of the adapter. Isn't it the same thing to create a view or viewgroup inside adapter or reference it to the outside? Or do I miss something overridding Layout methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论