0

次の問題があります。リストはポートレート モードでよく表示されます。しかし、横向きに切り替えると、アイテムの背景がスクランブル/逆順になります。

例えば:

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>

Androidマニフェスト

<?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>
4

2 に答える 2

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;
}
于 2013-02-20T12:12:20.020 に答える
0

さて、私は私の答えを見つけました。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;                        
          } 
}
于 2013-02-25T12:08:43.673 に答える