How to change the background of particular item of ListView programatically Android

断了今生、忘了曾经 提交于 2020-01-25 20:15:32

问题


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

I am getting this error

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

How to Solve this.


回答1:


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

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


来源:https://stackoverflow.com/questions/35455569/how-to-change-the-background-of-particular-item-of-listview-programatically-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!