在Android Studio中更新ListView适配器的方法如下:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List mData;
public MyAdapter(Context context, List data) {
mContext = context;
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(mData.get(position));
return convertView;
}
}
ListView listView = findViewById(R.id.listView);
List data = new ArrayList<>();
data.add("Item 1");
data.add("Item 2");
data.add("Item 3");
MyAdapter adapter = new MyAdapter(this, data);
listView.setAdapter(adapter);
data.add("Item 4");
adapter.notifyDataSetChanged();
这样就完成了ListView适配器的更新。当调用notifyDataSetChanged()方法后,ListView会重新调用适配器的getView()方法来更新显示的内容。