androidlistviewandroid-listviewgetview

Dynamically changing TextView color for Three different group in ListView


this is current output

I am trying to develop a ListView like a Index. In my case their are three level of list, chapter, subchapter and child.. successfully i implement three level.

I want to change three different color for all three list.

Tried:

  1. I tried to 3 level expentable listview but i cant able to achieve.

  2. so i tried Listview more dynamically with single array..

Now i want to change list color in listview by getview method

this is my getview() for adding subchapter to list:

            for (int i = listsubchapt.size() - 1; i >= 0; i--) {

                Mainarray.add(pos + 1, listsubchapt.get(i));
                Mainarrayid.add(pos + 1, listsubchaptid.get(i));

                //System.out.println("mainarraywithsub==++ " + Mainarray);
                ArrayAdapter<String> adapter = (new ArrayAdapter<String>(
                        IndexChapter.this, R.layout.singlerow,
                        R.id.textView1, Mainarray){@Override
                        public View getView(int position,
                                View convertView, ViewGroup parent) {

                            LayoutInflater mInflater = (LayoutInflater)getSystemService(
                                    Context.LAYOUT_INFLATER_SERVICE);
                         convertView = mInflater.inflate(R.layout.singlerow, null);

                         for(int i=0;i<Mainarray.size();i++){

                            String sample=Mainarray.get(i);
                            String[] items = sample.split(":"); 
                            if (items.length == 1) {
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.CYAN);

                            }else if(items.length == 2){
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.BLUE);
                            }else if(items.length == 3){
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.GRAY);
                            }else{
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.CYAN);
                            }
                         }
                            return super.getView(position, convertView, parent);
                        }});


                clientdetailslist.setAdapter(adapter);
                clientdetailslist.setSelection(pos-1);
                adapter.notifyDataSetChanged();

            } 

Edit :

its moving to length=1 block and changing all the values to cyan color.


Solution

  • Instance of Expentable listview you can use listview.

       @Override
       public View getView(int position,
         View convertView, ViewGroup parent) {
        LayoutInflater mInflater = (LayoutInflater)getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);
            View rowView = mInflater.inflate(R.layout.singlerow, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.textView1);
            RelativeLayout txt = ((RelativeLayout) rowView.findViewById(R.id.clicked));
            
            textView.setText(Mainarray.get(position));
            
            String s = Mainarray.get(position);
            String s2 = s.substring (1,7);
            String[] items = s2.split(":");
            if (items.length == 1) {
          //chapter color
          txt.setBackgroundColor(Color.WHITE);
          
         }else if(items.length == 2){
          //subchapter color
          txt.setBackgroundColor(Color.parseColor("#E6F5FF"));
         }else if(items.length == 3){
          //child color
          txt.setBackgroundColor(Color.parseColor("#CCEBFF"));
         }else{
          // else block if u need
          txt.setBackgroundColor(Color.MAGENTA);
         }
    
    
            return rowView;
    
       }}
    

    Now my Screen is:

    after changed color my screen