Search code examples
androidbackgroundandroid-listviewsimpleadapter

Problems on setting ListView's item background color


I've a ListView and when the user clicks on one of its items I want that item to become blue. In order to do this, in the onCreate() method of the ListView activity, I've set a listener for the user clicks.

m_listFile=(ListView)findViewById(R.id.ListView01);  
      m_listFile.setOnItemClickListener(new OnItemClickListener() {  

            public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {  
                arg0.getChildAt(arg2).setBackgroundColor(Color.BLUE);  
            }
});

Everything works fine for the first visible items, but when I scroll the list, I've a NullPointerException at arg0.getChildAt(arg2).setBackgroundColor(...), even if the arg2 value has the right item index position.

My ListView has a two lines item structure, when I load the ListView I use this adapter:

 SimpleAdapter sa = new SimpleAdapter(
            getApplicationContext(), 
            expsList, 
            R.layout.listelement, 
            new String[] { "screen_name","text" },
            new int[] { R.id.Name, R.id.Value}) {

      };

      m_listFile.setAdapter(sa);

I don't understand how to solve this problem. Can I get some help?


Solution

  • You could extend SimpleAdapter like this:

    private class MyAdapter extends SimpleAdapter {
    
            public MyAdapter(Context context, List<? extends Map<String, ?>> data,
                    int resource, String[] from, int[] to) {
                super(context, data, resource, from, to);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView,   parent);
                v.setBackgroundColor(Color.BLACK); //or whatever is your default color
                  //if the position exists in that list the you must set the background to BLUE
              if(pos!=null){
                if (pos.contains(position)) {
                    v.setBackgroundColor(Color.BLUE);
                }
              }
                return v;
            }
    
        }
    

    Then in your activity add a field like this:

    //this will hold the cliked position of the ListView
    ArrayList<Integer> pos = new ArrayList<Integer>();
    

    and set the adapter:

    sa = new MyAdapter(
                getApplicationContext(), 
                expsList, 
                R.layout.listelement, 
                new String[] { "screen_name","text" },
                new int[] { R.id.Name, R.id.Value}) {
    
          };
    m_listFile.setAdapter(sa);
    

    When you click the row:

        public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {  
                        // check before we add the position to the list of clicked positions if it isn't already set
                    if (!pos.contains(position)) {
                    pos.add(position); //add the position of the clicked row
                }
            sa.notifyDataSetChanged(); //notify the adapter of the change       
    }