notifyDataSetChanged的使用方法

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

    private Button button;
    private ListView listView;
    private List<String> datas;
    private ArrayAdapter<String> adapter;
    private int currentNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datas = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, datas);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                datas.add(currentNum+"");
                adapter.notifyDataSetChanged();
                currentNum++;
            }
        });
    }
}

你可能感兴趣的:(notifyDataSetChanged的使用方法)