我正在尝试在onSharedPreferenceChanged时更新我的ListView。我尝试了以下但是它不起作用:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if(!firstRun)
{
list.clear();
populateList();
}
firstRun = false;
}
问题是我不知道它们的关键数据已经发生变化,因为它可能是7种不同数据字符串之一。这是我想要更新的列表的活动:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
userInfo = this.getSharedPreferences(USERINFO_FILE, 0);
userInfo.registerOnSharedPreferenceChangeListener(this);
userControl = new UserController(context);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
list = new ArrayList<HashMap<String,String>>();
callForwardList = new SimpleAdapter(
this,
list,
R.layout.callforward_items,
new String[] { "line1","line2" },
new int[] { R.id.callforward_item_text, R.id.callforward_number } );
new PullInfoTask().execute();
setListAdapter( callForwardList );
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
switch(position)
{
case 0:
customDialogOnAlways();
break;
case 1:
customDialogOnBusy();
break;
case 2:
customDialogOnNoAnswer();
break;
}
}
});
}
编辑 populateList的代码
private void populateList()
{
cfinfo = userControl.GetCallForwardInfo();
if(Integer.parseInt( cfinfo[0]) == 1)
addItem("Viderestilling altid", cfinfo[1]);
else
addItem("Viderestilling altid", "Slået fra");
if(Integer.parseInt(cfinfo[2]) == 1)
addItem("Viderestilling ved optaget", cfinfo[3]);
else
addItem("Viderestilling ved optaget", "Slået fra");
if(Integer.parseInt(cfinfo[4]) == 1)
addItem("Viderestilling ved intet svar", cfinfo[5]);
else
addItem("Viderestilling ved intet svar","Slået fra");
callForwardList.notifyDataSetChanged();
}
private void addItem(String label, String number)
{
HashMap<String,String> item = new HashMap<String,String>();
item.put( "line1", label );
item.put( "line2", number );
list.add( item );
}
答案 0 :(得分:0)
您必须将变量列表创建为全局,以便callForwardList可以通过通知更改来获取它。以下是工作代码
SimpleAdapter callForwardList;
ArrayList<HashMap<String, String>> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
ListView lv = getListView();
list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item = new HashMap<String,String>();
item.put( "line1", "asas" );
item.put( "line2", "12121" );
list.add( item );
callForwardList = new SimpleAdapter(
this,
list,
R.layout.callforward_items,
new String[] { "line1","line2" },
new int[] { R.id.callforward_item_text, R.id.callforward_number } );
setListAdapter( callForwardList );
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
HashMap<String,String> item = new HashMap<String,String>();
item.put( "line1", "asas111" );
item.put( "line2", "1212111" );
list.add( item );
callForwardList.notifyDataSetChanged();
}
});
}
public void addf(View v)
{
HashMap<String,String> item = new HashMap<String,String>();
item.put( "line1", "asas111" );
item.put( "line2", "1212111" );
list.add( item );
callForwardList.notifyDataSetChanged();
}