繁体   English   中英

如何以编程方式更改ListView特定项目的背景

[英]How to change the background of particular item of ListView programatically Android

我正在创建一个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)”

如何解决这个问题。

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

        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);
                                    }
                                }
                            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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