简体   繁体   English

获取自定义列表适配器以正确膨胀多个线性布局

[英]Getting Custom List Adapter to properly inflate multiple linear layouts

I'm a little new to android and am trying to figure out these custom list adapter.我对 android 有点陌生,正在尝试找出这些自定义列表适配器。 So I have an activity that extends BaseListActivity and a function like:所以我有一个扩展 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);

And then I implement my custom WordsAdapter as follows:然后我实现我的自定义 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;
    }

}

From what I can tell from the logs is that getView is never being called.从日志中我可以看出,从未调用过 getView。 'single_word_container_item' is simply an xml file with a layout that I want to inflate multiple times. 'single_word_container_item' 只是一个 xml 文件,其布局我想多次膨胀。 I'm not sure about the entire process - I figured you set an adapter on your listview and then in that adapter you take care of displaying what will actually show up each time, so what am I doing wrong here?我不确定整个过程 - 我想你在你的列表视图上设置了一个适配器,然后在那个适配器中你负责显示每次实际显示的内容,那么我在这里做错了什么? Currently I'm getting a null pointer exception where I set my adapter but earlier it just would not show anything.目前,我在设置适配器时遇到了空指针异常,但早些时候它不会显示任何内容。 The logs show that it is getting into the WordsAdapter constructor but then dying.日志显示它正在进入 WordsAdapter 构造函数但随后死亡。 Thanks in advance for any advice.提前感谢您的任何建议。

EDIT: So it appears to be something wrong with identifying the listview in my xml.编辑:所以在我的 xml 中识别列表视图似乎有问题。

If you use:如果您使用:

ListView
android:id="@+id/android:list" OR android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
ListView

the error is a null pointer exception when you use it当您使用它时,错误是空指针异常

but if you define it as:但如果你将其定义为:

ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
ListView

you get the error: 07-15 19:18:50.240: E/AndroidRuntime(29842): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'你得到错误:07-15 19:18:50.240: E/AndroidRuntime(29842): Caused by: java.lang.RuntimeException: 你的内容必须有一个 id 属性为“android.R.id.list”的 ListView

EDIT: So apparently you can't extend listactivity and call setcontentview().编辑:显然你不能扩展 listactivity 并调用 setcontentview()。 Since I need a view on top of the listview I don't want to extend list activity but rather call setcontentview and just refer to the list by its id.由于我需要在列表视图之上的视图,因此我不想扩展列表活动,而是调用 setcontentview 并通过其 ID 引用列表。

Now my error is:现在我的错误是:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. java.lang.IllegalStateException: 适配器的内容已更改但 ListView 未收到通知。 Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.确保适配器的内容不是从后台线程修改的,而只是从 UI 线程修改的。

I tried calling adapter.notifyDataSetChanged() after my setAdapter() but that didn't seem to work.我尝试在我的 setAdapter() 之后调用 adapter.notifyDataSetChanged() 但这似乎不起作用。 Am I supposed to call it somewhere else?我应该在别处称呼它吗?

You have an error in your getView() method:你的 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() must be called on the row's view. findViewById() 必须在行的视图上调用。

Also look at the questionable logic above, what are you trying to do?也看看上面有问题的逻辑,你想做什么? This won't work too well if you are removing the listview objects directly from the adapter.如果您直接从适配器中删除列表视图对象,这将不会很好地工作。

只需使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM