Dev Qualwebs Dev Qualwebs - 1 year ago 112
Android Question

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

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.

enter image description here

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

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

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


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

listJobs = (ListView) Jobs.findViewById(;
button_ok = (Button) Jobs.findViewById(;
jobListViewAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayListJobs);
for(int i=0;i<arraySelectedJobs.size();i++)
int value = arrayListJobs.indexOf(arraySelectedJobs.get(i));
listJobs .getChildAt(value).setBackgroundColor(YELLOW);

catch(Exception ex)

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.

Answer Source

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); 
                                    CustomList adapter = new
                                    list = (ListView) dialogOne.findViewById(;

                                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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



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) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download