notifyDataSetChanged 更新內容
public class MainActivity extends Activity {
private String[] strGroup = {"字串1", "字串2", "字串3", "字串4", "字串5", "字串6"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, strGroup);
ListView list = new ListView(this);
list.setAdapter(adapter);
Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
strGroup[0] = "更改字串1";
strGroup[1] = "更改字串2";
adapter.notifyDataSetChanged();
}
});
LinearLayout container = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL);
container.addView(button);
container.addView(list);
setContentView(container);
}
}