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