[英]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.