在我的活動中我有:android gridview始終爲空
DrawView這是一個自定義的相對佈局。
這RelativeLayout的應該存在一個自定義導航欄和自定義的GridView的
自定義導航欄在這裏添加到drawView函數:
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.multiplechoice, null);
com.lernapp.src.Views.NavigationBarTest navigation = (com.lernapp.src.Views.NavigationBarTest)textEntryView.findViewById(R.id.navigationbar);
RelativeLayout parent = (RelativeLayout)navigation.getParent();
parent.removeAllViews();
drawView.addView(navigation);
我想做的事與GridView控件相似的東西..
我不明白,我的CustomGridView總是空,爲什麼這樣!?
MyCustomGridView grid = (MyCustomGridView)findViewById(R.id.gridview);
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.lernapp.src.Views.MyCustomGridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:listSelector="@android:color/transparent"
android:numColumns="auto_fit"
android:columnWidth="160dp"/>
</LinearLayout>
CustomGridView:
public class MyCustomGridView extends GridView implements OnItemClickListener{
private Listener mListener;
public MyCustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnItemClickListener(this);
}
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (mListener != null) {
mListener.onClick(position);
}
}
public void setListener(Listener l){
mListener = l;
}
public interface Listener{
void onClick(int position);
}
}
編輯:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int height = this.getWindowManager().getDefaultDisplay().getHeight();
int width = this.getWindowManager().getDefaultDisplay().getWidth();
MyCustomGridView grid = (MyCustomGridView)findViewById(R.id.gridview);
DrawView drawView = new DrawView(this, dragFieldText, targetFieldText, height, width, grid);
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.multiplechoice, null);
com.lernapp.src.Views.NavigationBarTest navigation = (com.lernapp.src.Views.NavigationBarTest)textEntryView.findViewById(R.id.navigationbar);
RelativeLayout parent = (RelativeLayout)navigation.getParent();
parent.removeAllViews();
drawView.addView(navigation);
setContentView(drawView);
}
您需要setContentView您的佈局在調用findViewById之前。你能發佈活動代碼嗎? – Heinrisch 2012-04-15 09:16:35
添加爲主帖子中的編輯 – krackmoe 2012-04-15 09:19:52
但是,我怎麼能在這裏做到這一點? 因爲我有一個自定義DrawView是一個自定義的相對佈局,並且在這個佈局中我想添加GridView ... – krackmoe 2012-04-15 09:22:34