[英]Getting Custom List Adapter to properly inflate multiple linear layouts
我對 android 有點陌生,正在嘗試找出這些自定義列表適配器。 所以我有一個擴展 BaseListActivity 的活動和一個函數,如:
public void inflate(){
ArrayList<String> words = new ArrayList<String>();
orders.add("hello");
orders.add("world");
wordsList = (ListView) findViewById(R.id.list);
WordsAdapter adapter = new WordsAdapter(this, R.layout.single_word_container_item,words);
wordsList.setAdapter(adapter);
然后我實現我的自定義 WordsAdapter 如下:
public class WordsAdapter extends ArrayAdapter<String>{
public Context context;
public ArrayList<String> _words;
//not sure the purpose of 'a' here
public WordsAdapter(Context context, int a, ArrayList<String> words) {
super(context, a, words);
_words = words;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.single_word_container_item, null);
TextView wordName = (TextView) findViewById(R.id.wordName);
if(!_words.isEmpty()){
wordName.setText(_word.remove(0));
}
return v;
}
}
從日志中我可以看出,從未調用過 getView。 'single_word_container_item' 只是一個 xml 文件,其布局我想多次膨脹。 我不確定整個過程 - 我想你在你的列表視圖上設置了一個適配器,然后在那個適配器中你負責顯示每次實際顯示的內容,那么我在這里做錯了什么? 目前,我在設置適配器時遇到了空指針異常,但早些時候它不會顯示任何內容。 日志顯示它正在進入 WordsAdapter 構造函數但隨后死亡。 提前感謝您的任何建議。
編輯:所以在我的 xml 中識別列表視圖似乎有問題。
如果您使用:
ListView
android:id="@+id/android:list" OR android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ListView
當您使用它時,錯誤是空指針異常
但如果你將其定義為:
ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ListView
你得到錯誤:07-15 19:18:50.240: E/AndroidRuntime(29842): Caused by: java.lang.RuntimeException: 你的內容必須有一個 id 屬性為“android.R.id.list”的 ListView
編輯:顯然你不能擴展 listactivity 並調用 setcontentview()。 由於我需要在列表視圖之上的視圖,因此我不想擴展列表活動,而是調用 setcontentview 並通過其 ID 引用列表。
現在我的錯誤是:
java.lang.IllegalStateException: 適配器的內容已更改但 ListView 未收到通知。 確保適配器的內容不是從后台線程修改的,而只是從 UI 線程修改的。
我嘗試在我的 setAdapter() 之后調用 adapter.notifyDataSetChanged() 但這似乎不起作用。 我應該在別處稱呼它嗎?
你的 getView() 方法有錯誤:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v;
if (convertView == null) {
v = inflater.inflate(R.layout.single_word_container_item, null);
} else {
v = convertView;
}
TextView wordName = (TextView) v.findViewById(R.id.wordName);
// questionable logic
if(!_words.isEmpty()){
wordName.setText(_word.remove(0));
}
return v;
}
findViewById() 必須在行的視圖上調用。
也看看上面有問題的邏輯,你想做什么? 如果您直接從適配器中刪除列表視圖對象,這將不會很好地工作。
只需使用val dialog = BottomSheetDialog(context,R.style.BottomSheetTheme)
val view = inflate(context,R.layout.otpsent1layout,null)
val dialog = BottomSheetDialog(context,R.style.BottomSheetTheme)
val view = inflate(context,R.layout.otpsent1layout,null)
dialog.setContentView(view)
dialog.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.