如何以编程方式更改ListView特定项目的背景 [英] How to change the background of particular item of ListView programatically Android

查看:96
本文介绍了如何以编程方式更改ListView特定项目的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a android application in which I am using a ListView on a dialog box. I want to change the background color of item on click and I have done this with the help of setOnItemClickListener.I am storing all selected values in a ListArray. I want to do like if user opens again that diolog box it must show what he has selected already according to data in ListArray. The exact problem is when I moved back to page and leave the dialogue box the list got renew and nothing shows selected.

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

This is how I show selected items. This is the code what I have used to do that...

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

            }
        });

当用户再次打开该对话框时,我试图显示所选的项目.

I am trying to show that selected item when user opens again that dialog box.

    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)'

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference

如何解决这个问题.

推荐答案

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

You need to create custom list view for your approach and use following code when you shows your dialog box

        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方法

Now in Custom list class where you are returning view for particular list row you need to write the following code to getview method

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

这篇关于如何以编程方式更改ListView特定项目的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆