填充ListView onWindowFocusChanged

时间:2012-03-01 14:14:50

标签: android listview android-asynctask

每当我按下我的手机时,我就会遇到麻烦,这会“禁用”我的列表视图,导致我无法按下它。我找到了一个可行的解决方案,它将重新填充onWindowFocusChanged上的listview,但是我在这个类中实现它时遇到了问题。我的populateListView方法在这个类中会怎么样?

public class AvailableNumbersActivity extends ListActivity // implements CallerIdListener
{
Context context;
UserController userControl;
private String[] newnumber = {""};
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  context = this;

  userControl = new UserController(context);
  ListView lv = getListView();
  lv.setTextFilterEnabled(true);
  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) 
    {
        newnumber[0] = ((TextView) view).getText().toString();
        new PushNumberTask().execute(newnumber);
    }
  });

  new PullNumbersTask(context).execute();

}

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);

    populateListView();
}

private void populateListView() {
    // TODO Auto-generated method stub

}

private class PullNumbersTask extends AsyncTask<Void, Void, String[]>
{
    private Context ctx; 

    public PullNumbersTask(Context context)
    {
        super();
        this.ctx=context;
    }
    private ProgressDialog Dialog = new ProgressDialog(getParent());

    protected void onPreExecute() 
    {
        Dialog.setMessage("Henter tilgængelige numre..");
        Dialog.show();
    }

    @Override
    protected String[] doInBackground(Void... arg0) {
        return (userControl.GetNumbers());
    }


     protected void onPostExecute(String[] result) 
     {
         setListAdapter(new ArrayAdapter<String>(ctx, R.layout.numbers_items, result));
         Dialog.dismiss();
     }



}

1 个答案:

答案 0 :(得分:0)

我不完全确定我完全理解了您的问题,但如果您需要刷新ListView,则应将ArrayAdapter保存为活动中的成员,并在notifyDataSetChanged()中调用onWindowFocusChanged {1}}处理程序。