[英]Android layout inflator from a Adapter class not working
我試圖從BaseAdapter擴展的ImageAdapter類中操縱布局的按鈕。
這是我的適配器類:
public class ImageAdapterAvatar extends BaseAdapter {
public ImageAdapterAvatar(Context context) {
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView = inflater.inflate(R.layout.avatar, null);
//do things with my gridwiew
View bouttonView = inflater.inflate(R.layout.activity_which_contain_my_button,null);
Button bouttonValidate = (Button) bouttonView.findViewById(R.id.buttonValidate);
}
}
和我的活動代碼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_which_contain_my_button);
fragmentAdaptateur = new ImageAdapterAvatar(this);
gridView可以工作,我可以做我想做的,但是按鈕不行,我可以獲取元素,但是當我嘗試像這樣操作時:
bouttonValidate.setText("hello world");
我沒有回應。
需要幫助~~
檢查一下,可能會有所幫助。
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (null == v) {
v = LayoutInflater.from(getContext()).inflate(R.layout.activity_which_contain_my_button, parent, false);
}else{
//null Log
}
return v;
}
最終我用這個招了
Activity activity= (Activity) context;
Button bouttonValidate = (Button) activity.findViewById(R.id.buttonValidate);
實際上,我可以簡單地將上下文轉換為活動,然后從活動中調用findViewById方法,然后操作布局元素
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.