2014-12-16 3 views
2

this is current outputДинамическое изменение TextView цвета для трех различных групп в ListView

Я пытаюсь разработать ListView вроде индекса. В моем случае они представляют собой три уровня списка, главы, подраздела и дочернего ... успешно реализую три уровня.

Я хочу изменить три разных цвета для всех трех списков.

Пробовал:

  1. Я попытался 3 уровня expentable ListView, но я не могу в состоянии достичь.

  2. поэтому я попытался Listview более динамически с одним массивом ..

Теперь я хочу, чтобы изменить список цвет в ListView методом GetView

это мой GetView() для добавления п в список:

  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:

его перемещение в длину = 1 блок и изменение всех значений до голубого цвета.

+0

, что ваша проблема? –

+0

@SweetWisher シ Я хочу дать три цвета разницы для главы, подглава и дочернего – prabhakaran

+0

, тогда что это делает 'txt.setBackgroundColor (Color.BLUE);'? –

ответ

0

Наконец я нашел ответ на мою проблему .. это может помочь кому-то .. Инстанции Expentable listview вы можете использовать listview .. ThanQ Eveyone ..

@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; 

    }} 

Теперь мой экран является:

after changed color my screen

Смежные вопросы