【问题标题】:How to change the background of particular item of ListView programatically Android如何以编程方式更改ListView特定项目的背景Android
【发布时间】:2016-02-17 11:34:58
【问题描述】:

我正在创建一个 android 应用程序,其中我在对话框上使用 ListView。我想在单击时更改项目的背景颜色,我在 setOnItemClickListener 的帮助下完成了这项工作。我将所有选定的值存储在 ListArray 中。如果用户再次打开该对话框,我想这样做,它必须根据 ListArray 中的数据显示他已经选择的内容。确切的问题是当我回到页面并离开对话框时,列表得到了更新并且没有显示任何选择。

这就是我显示所选项目的方式。 这是我用来做的代码......

listJobs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                selectedJob = a.getItemAtPosition(position).toString();
                if (!arraySelectedJobs.contains(selectedJob)) {
                    a.getChildAt(position).setBackgroundColor(YELLOW);
                    arraySelectedJobs.add(selectedJob);
                    Log.e("position", String.valueOf(position));
                } else {
                    a.getChildAt(position).setBackgroundColor(Color.WHITE);
                    arraySelectedJobs.remove(selectedJob);
                }

                Log.e("data", arraySelectedJobs.toString());

            }
        });

当用户再次打开该对话框时,我正在尝试显示该选定项目。

    listJobs = (ListView) Jobs.findViewById(R.id.listJobs123456);
            button_ok = (Button) Jobs.findViewById(R.id.ButtonOk);
            button_ok.setOnClickListener(this);
            jobListViewAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayListJobs);
            listJobs.setAdapter(jobListViewAdapter);
            if(!arraySelectedJobs.isEmpty())
            {
                for(int i=0;i<arraySelectedJobs.size();i++)
                {
                    try
                    {
                        int value = arrayListJobs.indexOf(arraySelectedJobs.get(i));
                        listJobs .getChildAt(value).setBackgroundColor(YELLOW);

                    }
                    catch(Exception ex)
                    {
                        Log.e("error",ex.toString());
                    }
                }
            }

我收到这个错误

java.lang.NullPointerException: 尝试调用虚方法 'void android.view.View.setBackgroundColor(int)' 在空对象引用上

如何解决这个问题。

【问题讨论】:

  • 当您选择任何工作然后离开对话框并再次打开它时发生错误,如果我错了,请纠正我
  • 您的代码是否在不离开对话框的情况下工作?
  • 是的,工作正常,不离开对话框你理解正确
  • 能否在日志中打印值 listJobs .getChildAt(value)
  • 确保这不应该为空

标签: android listview arraylist


【解决方案1】:

您需要为您的方法创建自定义列表视图,并在显示对话框时使用以下代码

        final Dialog dialogOne = new Dialog(MainActivity.this); 
        dialogOne.requestWindowFeature(Window.FEATURE_NO_TITLE);
                                    dialogOne.setContentView(R.layout.dialog_xml);
                                    CustomList adapter = new
                                            CustomList(MainActivity.this,arraySelectedJobs);
                                    list = (ListView) dialogOne.findViewById(R.id.list);
                                    list.setAdapter(adapter);

                                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view,
                                                                int position, long id) {

                                            parent.getChildAt(position).setBackgroundColor(Color.YELLOW);
                                            arraySelectedJobs.add(String.valueOf(position));

                                        }
                                    });
                                    dialogOne.show();

现在在自定义列表类中,您将返回特定列表行的视图 getview方法需要写如下代码

 LayoutInflater inflater = context.getLayoutInflater();
                            View rowView = inflater.inflate(R.layout.list_single, null, true);
                      if (!arraySelectedJobs.isEmpty()) {
                                for (int i = 0; i < arraySelectedJobs.size(); i++) {
                                    int j = Integer.parseInt(arraySelectedJobs.get(i));
                                    if (position == j) {
                                        rowView.setBackgroundColor(Color.YELLOW);
                                    }
                                }
                            }

【讨论】:

  • 谢谢你,我会尽力让你知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 2011-03-07
  • 2012-07-31
  • 1970-01-01
  • 2020-10-31
相关资源
最近更新 更多