2013-02-20 103 views
0

我有以下問題。 列表以縱向模式顯示。 但是,當我切換到風景,項目的背景獲取加擾/顛倒順序。Listview特殊適配器更改列背景方向更改

例如:

column 1 Column2(a) Column 3(b) Column 4(c) 
1   2(red)  3   4 
12  36(orange) 87(red)  32 
94  55   44   23(orange) 

現在我變成景觀和我的名單看起來是這樣的:

column 1 Column2(a) Column 3(b) Column 4(c) 
1   2    3   4(orange) 
12  36(orange) 87(red)  32 
94  55(red)  44   23 

數據保持不變,背景色切換。

我specialadapter裏面我有一個getView這樣的: SpecAdapter_list.java

public class SpecAdapter_list extends SimpleAdapter { 


     public SpecAdapter_list(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) { 
      super(context, items, resource, from, to); 
     } 
    @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      if (convertView == null) 
      { 

      // i have a listitem.xml wich lays-out each row in the list. 
      // several textviews are hidden. 
      // i check these hidden textviews on specific text. 
      // if it has the text, we change the background of a specific column item. 

       TextView c1 = (TextView) view.findViewById(R.id.c1); 
       String c1_text=(String)a.getText(); 
       TextView Columnitema = (TextView) view.findViewById(R.id.Columnitema);   
       if(c1_text.equals("YES")) 
       { 
        Columnitema.setBackgroundColor(0xFFe10000); 
        Columnitema.setTextColor(0xFFFFFFFF); 
       } 
       else if(c1_text.equals("NO")) 
       { 
        Columnitema.setBackgroundColor(0xFFff9600); 
        Columnitema.setTextColor(0xFFFFFFFF); 
       } 

       TextView c2 = (TextView) view.findViewById(R.id.c2); 
       String c2_text=(String)c2.getText(); 
       TextView Columnitemb = (TextView) view.findViewById(R.id.Columnitemb);   
       if(c2_text.equals("PERHAPS")) 
       { 
        Columnitemb.setBackgroundColor(0xFFe10000); 
        Columnitemb.setTextColor(0xFFFFFFFF); 
       } 
       else if(c2_text.equals("ABSOLUTELY")) 
       { 
        Columnitemb.setBackgroundColor(0xFFff9600); 
        Columnitemb.setTextColor(0xFFFFFFFF); 
       } 
       TextView c3 = (TextView) view.findViewById(R.id.c3); 
       String c3_text=(String)c3.getText(); 
       TextView Columnitemc = (TextView) view.findViewById(R.id.Columnitemc);   
       if(c3_text.equals("PERHAPS")) 
       { 
        Columnitemc.setBackgroundColor(0xFFe10000); 
        Columnitemc.setTextColor(0xFFFFFFFF); 
       } 
       else if(c3_text.equals("ABSOLUTELY")) 
       { 
        Columnitemc.setBackgroundColor(0xFFff9600); 
        Columnitemc.setTextColor(0xFFFFFFFF); 
       }     
      } 
      return view; 
     } 
    } 

MainActivity.java

package com.z_list_columnitembackground; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import com.z_list_columnitembackground.R; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.ListView; 

public class MainActivity extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
      ListView lv= (ListView)findViewById(R.id.listview); 

      // create the grid item mapping 
      String[] from = new String[] {"rowid", "col_1", "col_2", "col_3", "c1_status","c2_status","c3_status"}; 
      int[] to = new int[] { R.id.LI_Number, R.id.Columnitema, R.id.Columnitemb, R.id.Columnitemc, R.id.c1, R.id.c2, R.id.c3 }; 

      // prepare the list of all records 
      List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 
      // for(int i = 0; i < 10; i++){ 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("rowid", "1"); 
       map.put("col_1", "2"); 
       map.put("col_2", "3"); 
       map.put("col_3", "4"); 
       map.put("c1_status", "YES"); 
       map.put("c2_status", ""); 
       map.put("c3_status", ""); 
       fillMaps.add(map); 

       HashMap<String, String> map2 = new HashMap<String, String>(); 
       map2.put("rowid", "12"); 
       map2.put("col_1", "36"); 
       map2.put("col_2", "87"); 
       map2.put("col_3", "32"); 
       map2.put("c1_status", "NO"); 
       map2.put("c2_status", "PERHAPS"); 
       map2.put("c3_status", ""); 
       fillMaps.add(map2); 

       HashMap<String, String> map3 = new HashMap<String, String>(); 
       map3.put("rowid", "94"); 
       map3.put("col_1", "55"); 
       map3.put("col_2", "44"); 
       map3.put("col_3", "23"); 
       map3.put("c1_status", ""); 
       map3.put("c2_status", ""); 
       map3.put("c3_status", "ABSOLUTELY"); 
       fillMaps.add(map3); 
      // } 

      // fill in the grid_item layout 
      SpecAdapter_list adapter = new SpecAdapter_list(this, fillMaps, R.layout.list_item, from, to); 
      lv.setAdapter(adapter);  
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 

的strings.xml

<string name="app_name">Z_list_alternatingcolors</string> 
    <string name="menu_settings">Settings</string> 

    <string name="LI_Number">1</string> 
    <string name="Columnitema">dummya</string> 
    <string name="Columnitemb">dummyb</string> 
    <string name="Columnitemc">dummyc</string> 
</resources> 

AndroidManifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.z_list_columnitembackground" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="10" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.z_list_columnitembackground.MainActivity" 
      android:label="@string/app_name" 
      android:windowSoftInputMode="stateHidden|adjustResize" 
      android:configChanges="keyboardHidden|orientation"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

回答

0

w ^我找到了我的答案。 這裏正確的代碼爲SpecAdapter_list.java

package com.z_list_columnitembackground; 

import java.util.HashMap; 
import java.util.List; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

public class SpecAdapter_list extends SimpleAdapter { 
private int[] colors = new int[] { 0x30FF0000, 0x300000FF }; 


    private LayoutInflater mInflater; 
    private final List<HashMap<String, String>> names; 
    private int id; 


     public SpecAdapter_list(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) { 
      super(context, items, resource, from, to); 
      mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      this.names = items; 
      this.id=resource; 
      } 
     @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
     { 
      ViewHolder holder = null; 
     if (convertView == null) 
      { 
       holder = new ViewHolder(); 
       convertView = mInflater.inflate(id, null); 
       holder.c1 = (TextView)convertView.findViewById(R.id.c1); 
       holder.c2 = (TextView)convertView.findViewById(R.id.c2); 
       holder.c3 = (TextView)convertView.findViewById(R.id.c3); 
       holder.LI_Number = (TextView)convertView.findViewById(R.id.LI_Number); 
       holder.Columnitema = (TextView)convertView.findViewById(R.id.Columnitema); 
       holder.Columnitemb = (TextView)convertView.findViewById(R.id.Columnitemb); 
       holder.Columnitemc = (TextView)convertView.findViewById(R.id.Columnitemc); 
       convertView.setTag(holder); 
      } 
      else 
      { 
       holder = (ViewHolder)convertView.getTag(); 
      } 

      //set background color of the whole listitem. 
      View view = super.getView(position, convertView, parent); 
      int colorPos = position % colors.length; 
      view.setBackgroundColor(colors[colorPos]); 


      //set data in correct fields 
      final HashMap<String, String> o = names.get(position); 
      holder.c1.setText(o.get("c1_status")); 
      holder.c2.setText(o.get("c2_status")); 
      holder.c3.setText(o.get("c3_status")); 
      holder.LI_Number.setText(o.get("rowid")); 
      holder.Columnitema.setText(o.get("col_1")); 
      holder.Columnitemb.setText(o.get("col_2")); 
      holder.Columnitemc.setText(o.get("col_3")); 

      //get hidden field, check for value and give that field a backgroundcolor 
      TextView c1 = (TextView) convertView.findViewById(R.id.c1); 
      String c1_text=(String)c1.getText(); 
      TextView Columnitema = (TextView) convertView.findViewById(R.id.Columnitema);   
      if(c1_text.equals("YES")) 
      { 
       Columnitema.setBackgroundColor(0xFFe10000); 
       Columnitema.setTextColor(0xFFFFFFFF); 
      } 
      else if(c1_text.equals("NO")) 
      { 
       Columnitema.setBackgroundColor(0xFFff9600); 
       Columnitema.setTextColor(0xFFFFFFFF); 
      } 
      else 
      { 
       Columnitema.setBackgroundColor(0x00000000); 
       Columnitema.setTextColor(0xFF000000);    
      } 
      TextView c2 = (TextView) convertView.findViewById(R.id.c2); 
      String c2_text=(String)c2.getText(); 
      TextView Columnitemb = (TextView) convertView.findViewById(R.id.Columnitemb);   
      if(c2_text.equals("PERHAPS")) 
      { 
       Columnitemb.setBackgroundColor(0xFFe10000); 
       Columnitemb.setTextColor(0xFFFFFFFF); 
      } 
      else if(c2_text.equals("ABSOLUTELY")) 
      { 
       Columnitemb.setBackgroundColor(0xFFff9600); 
       Columnitemb.setTextColor(0xFFFFFFFF); 
      } 
      else 
      { 
       Columnitemb.setBackgroundColor(0x00000000); 
       Columnitemb.setTextColor(0xFF000000);    
      }   
      TextView c3 = (TextView) convertView.findViewById(R.id.c3); 
      String c3_text=(String)c3.getText(); 
      TextView Columnitemc = (TextView) convertView.findViewById(R.id.Columnitemc);   
      if(c3_text.equals("PERHAPS")) 
      { 
       Columnitemc.setBackgroundColor(0xFFe10000); 
       Columnitemc.setTextColor(0xFFFFFFFF); 
      } 
      else if(c3_text.equals("ABSOLUTELY")) 
      { 
       Columnitemc.setBackgroundColor(0xFFff9600); 
       Columnitemc.setTextColor(0xFFFFFFFF); 
      } 
      else 
      { 
       Columnitemc.setBackgroundColor(0x00000000); 
       Columnitemc.setTextColor(0xFF000000);    
      }   

      return convertView; 
     } 

    //a viewholder class for each field inside list_item.xml 
    public static class ViewHolder { 
      public TextView c1;  
      public TextView c2;   
      public TextView c3;  
      public TextView LI_Number;  
      public TextView Columnitema;   
      public TextView Columnitemb;  
      public TextView Columnitemc;       
      } 
} 
0

嘗試

View view = convertView; 

代替:

View view = super.getView(position, convertView, parent); 

編輯:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if(view == null){ 
     LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.your_layout, null); 
     } 
// i have a listitem.xml wich lays-out each row in the list. 
    // several textviews are hidden. 
    // i check these hidden textviews on specific text. 
    // if it has the text, we change the background of a specific column item. 

     TextView c1 = (TextView) view.findViewById(R.id.c1); 
     String c1_text=(String)a.getText(); 
     TextView Columnitema = (TextView) view.findViewById(R.id.Columnitema);   
     if(c1_text.equals("YES")) 
     { 
      Columnitema.setBackgroundColor(0xFFe10000); 
      Columnitema.setTextColor(0xFFFFFFFF); 
     } 
     else if(c1_text.equals("NO")) 
     { 
      Columnitema.setBackgroundColor(0xFFff9600); 
      Columnitema.setTextColor(0xFFFFFFFF); 
     } 

     TextView c2 = (TextView) view.findViewById(R.id.c2); 
     String c2_text=(String)c2.getText(); 
     TextView Columnitemb = (TextView) view.findViewById(R.id.Columnitemb);   
     if(c2_text.equals("PERHAPS")) 
     { 
      Columnitemb.setBackgroundColor(0xFFe10000); 
      Columnitemb.setTextColor(0xFFFFFFFF); 
     } 
     else if(c2_text.equals("ABSOLUTELY")) 
     { 
      Columnitemb.setBackgroundColor(0xFFff9600); 
      Columnitemb.setTextColor(0xFFFFFFFF); 
     } 
     TextView c3 = (TextView) view.findViewById(R.id.c3); 
     String c3_text=(String)c3.getText(); 
     TextView Columnitemc = (TextView) view.findViewById(R.id.Columnitemc);   
     if(c3_text.equals("PERHAPS")) 
     { 
      Columnitemc.setBackgroundColor(0xFFe10000); 
      Columnitemc.setTextColor(0xFFFFFFFF); 
     } 
     else if(c3_text.equals("ABSOLUTELY")) 
     { 
      Columnitemc.setBackgroundColor(0xFFff9600); 
      Columnitemc.setTextColor(0xFFFFFFFF); 
     }     
    } 
    return view; 
} 
+0

這並不起作用,因爲convertView是空的好幾倍。 – user2064635 2013-02-20 12:33:18

+0

@ user2064635:我編輯了我的答案 – Shoshi 2013-02-20 12:45:23

+0

對不起,我是android編程新手。此代碼位於擴展simpleadapter的自定義specialadapter中。這個適配器被多個頁面使用。因此,我不能把R.layout.your_layout和getActivity()未定義。請多一點幫助。 – user2064635 2013-02-20 13:25:56