我在Kitkat中遇到Switch问题。我有一个屏幕,其页脚视图和页脚视图中的开关。该开关在onCreate事件中作为页脚添加到listview中,然后使用扩展BaseAdapter的SmsListAdapter在onResume事件中异步填充listview。我已经添加了两个toast消息来切换onCheckedChanged事件以指示当前的开关状态(ON | OFF)。
该开关在较低版本的Android手机(4至4.3)中运行良好。但是在Kitkat中,当屏幕加载时,它最初工作得很好。但是如果我锁定并解锁屏幕并尝试将其打开和关闭,我只会从ON到OFF获得相应的Toast消息,反之亦然。但是当我解锁手机时,开关显示器仍然处于状态。如果我更改手机的方向,则会正确更新显示。我用3种不同的Kitkat手机(Moto G,Moto E和Karbonn Sparkle v)验证了这一点。但是在另一个屏幕中的类似开关在类似条件下工作正常,但它不是任何列表视图的一部分。我做错了吗?有人请帮我解决这个问题。
public class Reminders extends Activity {
private boolean headerAdded = false;
DatabaseAdapter f_dbhelper=null;
Cursor f_cursor=null;
ListView f_remlist=null;
SmsListAdapter f_adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
try {
f_remlist = (ListView)findViewById(R.id.reminders_list_1);
if(!headerAdded) {
//add header and footer
LayoutInflater l_inflater = getLayoutInflater();
ViewGroup l_footer = ViewGroup)l_inflater.inflate(R.layout.reminder_footer, f_remlist, false);
f_remlist.addFooterView(l_footer, null, false);
f_remlist.setAdapter(null);
headerAdded = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
try {
MySmsList l_mylist = new MySmsList();
l_mylist.execute("start");
} catch (Throwable e) {
}
}
}
private class MySmsList extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... l_params) {
String l_status="completed";
try {
Calendar l_cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM",new Locale("en","US"));
String remDate = dateFormat.format(l_cal.getTime());
f_dbhelper = new DatabaseAdapter(Reminders.this);
f_dbhelper.open();
f_cursor = f_dbhelper.selectAllReminders();
rowCount = f_cursor.getCount();
f_adapter = new SmsListAdapter(Reminders.this,f_cursor,remDate);
f_adapter.notifyDataSetChanged();
} catch(Throwable e) {
e.printStackTrace();
}
return l_status;
}
@Override
protected void onPostExecute(String l_result) {
try {
f_remlist.setAdapter(f_adapter);
} catch(Throwable e) {
e.printStackTrace();
}
showMyQuoteSwitch();
}
@TargetApi(14)
private void showMyQuoteSwitch() {
Switch switchQuote = (Switch)findViewById(R.id.my_quote_switch);
switchQuote.setChecked(f_pref.getBoolean1("R.id.switch_layer_quote"));
switchQuote.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg1) {
Toast.makeText(this, R.string.reminder_on, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.reminder_off, Toast.LENGTH_SHORT).show();
}
}
});
}
reminder_footer.xml:
<RelativeLayout
android:id="@+id/rem_quote_layer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
>
<Switch
android:id="@+id/my_quote_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/none"
android:checked="true"
/>
</RelativeLayout>