我有以下問題。 列表以縱向模式顯示。 但是,當我切換到風景,項目的背景獲取加擾/顛倒順序。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>
這並不起作用,因爲convertView是空的好幾倍。 – user2064635 2013-02-20 12:33:18
@ user2064635:我編輯了我的答案 – Shoshi 2013-02-20 12:45:23
對不起,我是android編程新手。此代碼位於擴展simpleadapter的自定義specialadapter中。這個適配器被多個頁面使用。因此,我不能把R.layout.your_layout和getActivity()未定義。請多一點幫助。 – user2064635 2013-02-20 13:25:56