2013-12-17 39 views
0

我正在開發一個應用程序,它具有項目列表,並從列表視圖中選擇特定項目,我想更改所選整行的背景顏色,但在實現此操作時,我的所有行背景顏色改變。請有人幫助我。感謝Advace。這裏是我的Adaptor.xml更改列表視圖的特定行的背景

public class Adaptor_ListItem extends ArrayAdapter<MyItem> { 
     public Context mContext; 
     public ArrayList<MyItem> listItem; 
     public LayoutInflater inflater; 
     public int position1=-1; 

     public Adaptor_ListItem(Context context, int resource, List<MyItem> list, 
       int selectedPos) { 
      super(context, resource, list); 
      // TODO Auto-generated constructor stub 
      mContext = context; 
      listItem = (ArrayList<MyItem>) list; 
      inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      position1 = selectedPos; 
     } // method ends 

     public View getView(int pos, View convertView, ViewGroup parent) { 
      View holder = convertView; 
      if (holder == null) { 
       holder = inflater.inflate(R.layout.adaptor_itemlist, null); 
      } 
      if (listItem.size() != 0) { 

       TextView txtName = (TextView) holder 
         .findViewById(R.id.ListItem_txtName); 
       TextView txtDays = (TextView) holder 
         .findViewById(R.id.ListItem_txtDays); 
       TextView txtRecurring = (TextView) holder 
         .findViewById(R.id.ListItem_txtRecuring); 
       MyItem objItem = listItem.get(pos); 
       if (objItem != null) { 
        String strName = objItem.itemName; 
        String strRecurring = objItem.recurring + ""; 
        String strDays = objItem.days; 
        int itemId = objItem.itemId; 
        // for checking which item has notified 
        if ((pos== position1) && holder!=null) { 
         LinearLayout linearLayout = (LinearLayout) holder 
           .findViewById(R.id.linearListItem); 
         txtName.setBackgroundColor(Color.RED); 
         txtDays.setBackgroundColor(Color.RED); 
         txtRecurring.setBackgroundColor(Color.RED); 
         System.out.println("Adaptor_ListItem.getView()//// method block"); 
        } 
        if (strDays == null) { 
         txtName.setText(strName); 
         txtDays.setText("0 Days"); 
         txtRecurring.setText(strRecurring + ""); 
        } else { 
         txtName.setText(strName); 
         txtDays.setText(strDays + " Days"); 
         txtRecurring.setText(strRecurring + ""); 

        } 

       } 
      } 
      return holder; 
     }// method ends 
    } // final class ends 

回答

0

只要提到adaptor_itemlist.xml文件中主佈局的背景。

編寫背景選擇:

item_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:state_selected="true" android:drawable="@color/red"></item> 
<item android:state_selected="false" android:drawable="@android:color/white"></item> 

</selector> 

adapter_itemlist.xml

使用婁代碼應用此文件主要佈局的背景:

linearLayout.setSelected(true); 

代替:

txtName.setBackgroundColor(Color.RED); 
txtDays.setBackgroundColor(Color.RED); 
txtRecurring.setBackgroundColor(Color.RED); 
+0

實際上這不是我需要的, –

+0

其中您正在更改..可以更新您的onItemClickListener()。 – user543

+0

Shayan pourvatan:實際上我爲listview的特定項目添加通知,當接收到notifcation時,我打開具有此listview的同一活動,並通過intent添加選定行的位置,當此活動打開時,我獲得此位置並通過它適配器的構造函數設置一些背景顏色到這一行,我通過這種方式來處理if(pos == position1),但它改變了所有行的背景顏色,這是我的問題,它應該只改變背景顏色選定的行。 –

0

在您的自定義適配器的getview中添加此行。

listTextView.setTextColor(Color.parseColor("#aaaaaa")); 
+0

背景顏色在我的代碼中變化,但不是單行,它是所有行都在變化,這是我的實際問題 –

+0

我的意思是你說所有行都有不同的顏色。 – dipali

+0

請把顏色和通過數組傳遞到您的位置的自定義適配器基地.. – dipali

0

使用下面的代碼,而不是你的代碼:

 public View getView(int pos, View convertView, ViewGroup parent) { 
     View v = convertView; 

     ViewHolder Holder; 

    if (v == null) { 
      v = inflater.inflate(R.layout.adaptor_itemlist, null); 

      Holder = new ViewHolder(); 

      Holder.txtName = (TextView) v 
        .findViewById(R.id.ListItem_txtName); 
      Holder.txtDays = (TextView) v 
        .findViewById(R.id.ListItem_txtDays); 
      Holder.txtRecurring = (TextView) v 
        .findViewById(R.id.ListItem_txtRecuring); 
      MyItem objItem = listItem.get(pos); 
      v.setTag(Holder); 
      } 
     else 
     holder=(ViewHolder)v.getTag(); 

     if (objItem != null) { 
       String strName = objItem.itemName; 
       String strRecurring = objItem.recurring + ""; 
       String strDays = objItem.days; 
       int itemId = objItem.itemId; 
       // for checking which item has notified 
       if ((pos== position1)) { 
        LinearLayout linearLayout = (LinearLayout) v 
          .findViewById(R.id.linearListItem); 
        Holder.txtName.setBackgroundColor(Color.RED); 
        Holder.txtDays.setBackgroundColor(Color.RED); 
        Holder.txtRecurring.setBackgroundColor(Color.RED); 
        System.out.println("Adaptor_ListItem.getView()//// method block"); 
       } 
     else 
     { 
        // set Default Value 
       } 
       if (strDays.equals(null)) { 
        Holder.txtName.setText(strName); 
        Holder.txtDays.setText("0 Days"); 
        Holder.txtRecurring.setText(strRecurring + ""); 
       } else { 
        Holder.txtName.setText(strName); 
        Holder.txtDays.setText(strDays + " Days"); 
        Holder.txtRecurring.setText(strRecurring + ""); 

       } 


     } 
     return v; 
    }// method ends 
} // final class ends 

和ViewHolder類:

public static class ViewHolder() 
{ 
    public TextView txtName , txtDays, txtRecurring; 
    } 
+0

什麼是錯誤?你的意思是不工作? –

0
int [] arrayColors={Color.GREEN,Color.BLUE,Color.Red} 

此線以下呼叫您的適配器你getview方法。

yourView.setBackgroundColor(Color.parseColor(arrayColors[pos])); 
+0

不要把條件。這個代碼直接適用 – dipali

+0

你不覺得這得到''IndexOut'在'arrayColors'?你只能在你的數組中放入三個項目,但是'pos'可能大於3. –

+0

這是基於需求howmany顏色在數組中存在..這不是問題...任何人都可以理解我的應用程序需要哪個數組大小。? – dipali

0

當(pos!= position1)..或您的if(pos = position1)語句的else子句時,需要將背景色設置回原來的顏色。現在發生的事情是,一旦將列表項設置爲紅色,它將被回收到另一行,但它保持爲紅色,因爲它在新的getView()調用中從未取消設置。

   int feedbackColor = Color.WHITE; // or whatever your base color is 
       if ((pos== position1) && holder!=null) { 
        LinearLayout linearLayout = (LinearLayout) holder 
          .findViewById(R.id.linearListItem); 
        feedbackColor = Color.RED; 

       } 
       txtName.setBackgroundColor(feedbackColor); 
       txtDays.setBackgroundColor(feedbackColor); 
       txtRecurring.setBackgroundColor(feedbackColor);