从ArrayAdapter刷新ListView

时间:2012-08-02 21:27:24

标签: android android-listview android-arrayadapter

我正在使用ArrayAdapter将我的ArrayList数据绑定到ListView,我使用AlertDialog将数据插入我的Arraylist。我的问题是,在完成更改后我无法刷新ListView

代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.config_hidden);
    listView=(ListView) findViewById(R.id.hiddenList);
    xmlFileManager=new XmlFileManager(this);
    addNumber=(Button) findViewById(R.id.addNum);

    addNumber.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            LayoutInflater factory = LayoutInflater.from(HiddenCall.this);
            final View alertDialogView = factory.inflate(R.layout.add_number, null);
            AlertDialog.Builder adb = new AlertDialog.Builder(HiddenCall.this);
            adb.setView(alertDialogView);
            adb.setTitle(R.string.dialog_title);
            adb.setIcon(R.drawable.phone);

            final AlertDialog alertDialog = adb.create();

            adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    numberToAdd=(EditText) alertDialogView.findViewById(R.id.numberToAdd);
                    String number = numberToAdd.getText().toString();
                    if(number.length()>0){
                        xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
                        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
                        adapter.setNotifyOnChange(true);
                        adapter.notifyDataSetChanged();
                    }
                } });

            adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                } });
            adb.show();
        }
    });
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
    adapter.notifyDataSetChanged();
    adapter.setNotifyOnChange(true);
    listView.setAdapter(adapter);
}

4 个答案:

答案 0 :(得分:7)

您每次都要实例化一个新适配器。您需要做的是在单击侦听器之前放置实例化适配器的行,并在单击侦听器中修改该适配器并在其上调用notifyDataSetChanged()。您当然必须在适配器中添加一些setter才能修改数据。

必须与此类似:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config_hidden);
listView=(ListView) findViewById(R.id.hiddenList);

//instantiate the adapter (just one time)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());

//assign the adapter to the listview
listView.setAdapter(adapter);

xmlFileManager=new XmlFileManager(this);
addNumber=(Button) findViewById(R.id.addNum);

addNumber.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

        LayoutInflater factory = LayoutInflater.from(HiddenCall.this);
        final View alertDialogView = factory.inflate(R.layout.add_number, null);
        AlertDialog.Builder adb = new AlertDialog.Builder(HiddenCall.this);
        adb.setView(alertDialogView);
        adb.setTitle(R.string.dialog_title);
        adb.setIcon(R.drawable.phone);

        final AlertDialog alertDialog = adb.create();



        adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                numberToAdd=(EditText) alertDialogView.findViewById(R.id.numberToAdd);
                String number = numberToAdd.getText().toString();
                if(number.length()>0){
                    xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
                    //adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
                    //adapter.setNotifyOnChange(true);

                    //set the changed data
                    adapter.setData(xmlFileManager.getHiddenNumbers());

                    //notify that the model changed
                    adapter.notifyDataSetChanged();
                }
            } });

        adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            } });
        adb.show();
    }
});

//adapter.notifyDataSetChanged();
//adapter.setNotifyOnChange(true);

答案 1 :(得分:1)

如果您有一个小的ArrayList,要在ArrayList更改时强制更新ListView,您可以使用此代码段:

ArrayAdapter.clear();
ArrayAdapter.addAll(ArrayList);
ArrayAdapter.notifyDataSetChanged();

答案 2 :(得分:0)

每次点击都会创建新适配器。你不需要这样做。您可以修改现有适配器数据并调用notifyDataSetChanged()。在您的情况下,您应该在onClick方法中调用listView.setAdapter(adapter)。

答案 3 :(得分:0)

adb.setPositiveButton()的{​​{1}}内,您不应该创建新的适配器。删除这一行:

onClick()

只需这样做:

adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());