【问题标题】:Calling getChildAt() method of ListView after setting Adapter设置Adapter后调用ListView的getChildAt()方法
【发布时间】:2023-03-12 11:49:01
【问题描述】:

我在调用 ListView 的 setAdapter() 后尝试运行 ListView.getChildAt() 方法,但它给了我 NullPointerException。似乎设置适配器不会导致创建子视图。正如下面的方法所说,我正在尝试获取子视图,以便可以更改其背景颜色。我该如何解决这个问题?

private void showAnswers(int questionLocation)
{
    List<Answer> answers = getAnswersByQuestionLocation(questionLocation);

    ArrayAdapter<String> answerAdapter = new ArrayAdapter<String>(this,
                                    android.R.layout.simple_list_item_activated_1);

    for (int i = 0; i < answers.size(); i++)
    {
        answerAdapter.add(mOptionLetters[i] +". "+ answers.get(i).getAnswerText());
    }

    mAnswerList.setAdapter(answerAdapter);

    if (mAnswerLocationByQuestionLocation.indexOfKey(questionLocation) > -1)
    {
        Log.v("Child Count",String.valueOf(mAnswerList.getChildCount()));

            //mAnswerList.getChildAt(
            //       mAnswerLocationByQuestionLocation.get(questionLocation))
            //      .setSelected(true);
    }
}

【问题讨论】:

    标签: java android android-listview nullpointerexception


    【解决方案1】:

    如果你只需要改变背景, 这里是如何工作的:

        ArrayAdapter<String> answerAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_activated_1) {
            @Override
            public View getView(int position, View convertView,
                    ViewGroup parent) {
                View v =super.getView(position, convertView, parent);;
                v.setBackgroundColor(Color.RED);
                return v;
            }
    
        };
    

    因为你在 UI 线程上运行,listView 需要足够的时间在线程的下一个循环中创建视图,但你无法猜测何时。 所以最好的方法是在适配器中覆盖getView,因为适配器为 listView 提供子项:)


    class MyModel {
    
               public MyMode(String txt){
                 this.txt = txt
                }
    
            public String txt;
            public boolean isSelected;
    
            @Override
            public String toString() {
                return txt;
            }
        }
    

    ArrayAdapter<MyModel> answerAdapter = new ArrayAdapter<MyModel>(this,
            android.R.layout.simple_list_item_activated_1) {
        @Override
        public View getView(int position, View convertView,
                ViewGroup parent) {
            View v =super.getView(position, convertView, parent);;
            MyModel  model = getItem(position);
            if(model.isSelected){
               v.setBackgroundColor(Color.RED);} 
             else{
               v.setBackgroundColor(Color.WHITE);}
            return v;
        }
    
    };
    

    answerAdapter.add(new MyModel(mOptionLetters[i] +". "+ answers.get(i).getAnswerText()));
    

    if (mAnswerLocationByQuestionLocation.indexOfKey(questionLocation) > -1)
        {
            MyModel model = (MyModel)mAnswerList.getItemAtPosition(questionLocation);
        model.isSelected= true;
        answerAdapter.notifyDataSetChanged();
    
        }
    }
    

    无论如何你需要阅读这个关于 ListViews 和 Adapters 的教程 http://www.vogella.com/articles/AndroidListView/article.html

    【讨论】:

    • 不。当有人单击导航页面上的下一个或上一个按钮时,我正在更改背景,以便可以保留所选答案并向用户显示。
    • 因此,您必须使用Modelflag 表明已选择此答案,然后通知适配器重新创建视图。
    • 刷新答案:)
    • 我猜你还需要在getView方法中设置TextView文本字段。
    • 是的,如果您使用的是 BaseAdapter,但在您的情况下,ArrayAdapter 会为您增加 android.R.layout.simple_list_item_activated_1 中的视图
    猜你喜欢
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多