我試圖在我的片段的列表視圖中打印數組字符串[],我總是得到一個錯誤。這是我的片段類:在Android上使用ListView或TextView與ArrayAdapter?
public class Fragment_Database extends Fragment
public Fragment_Database() {
// Constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_database, container, false);
ListView list;
list = (ListView) v.findViewById(R.id.listView1);
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
"Ubuntu", "Solaris", "Android", "iPhone"};
list.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.fragment_database, names));
return v;
}
}
這裏的XML:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
如果我把一個ListView的XML我得到這個錯誤:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
如果我把一個TextView放在XML中,我得到這個:
android.widget.TextView cannot be cast to android.widget.ListView
謝謝! :D它的工作原理...我應該使用哪種佈局,如果我想要一個gridView而不是simplee_list ...?你知道嗎?我正在關閉此問題:P – Jaythaking
我沒有使用GridView。但我可以輕鬆地在這個頁面。 [link] http://androidexample.com/Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=75&aaid=99 – Amadas
謝謝你的好意 – Jaythaking