我遇到notifyDataSetChanged()
的问题。
这是处理ListView的类。
public class SummonersActivity extends Activity {
private ListView summonersList;
private SummonerListAdapter adapter;
private DatabaseHandler databaseHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_summoners);
databaseHandler = new DatabaseHandler(getBaseContext());
summonersList = (ListView) findViewById(R.id.summonersList);
adapter = new SummonerListAdapter(this, databaseHandler);
summonersList.setAdapter(adapter);
}
public void addSummoner(View view) {
Intent myIntent = new Intent(this, Login.class);
startActivity(myIntent);
}
@Override
protected void onResume(){
super.onResume();
adapter.swapItems(databaseHandler.getAllSummoners());
adapter.notifyDataSetChanged();
}
}
这是我制作的自定义适配器
public class SummonerListAdapter extends ArrayAdapter<Summoner> {
private final Context context;
private List<Summoner> itemsArrayList;
private final DatabaseHandler databaseHandler;
public SummonerListAdapter(Context context, DatabaseHandler databaseHandler) {
super(context, R.layout.summoner_list_row, databaseHandler.getAllSummoners());
this.context = context;
this.itemsArrayList = databaseHandler.getAllSummoners();
this.databaseHandler=databaseHandler;
}
public void update(){
this.itemsArrayList.clear();
this.itemsArrayList=databaseHandler.getAllSummoners();
this.notifyDataSetChanged();
}
public void swapItems(List<Summoner> items) {
this.itemsArrayList = items;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.summoner_list_row, parent, false);
final ImageView iconView = (ImageView) rowView.findViewById(R.id.summonerIcon);
TextView summonerName = (TextView) rowView.findViewById(R.id.summonerName);
TextView region = (TextView) rowView.findViewById(R.id.region);
ImageButton checkBtn = (ImageButton) rowView.findViewById(R.id.checkBtn);
Icon icon = databaseHandler.getIcon(itemsArrayList.get(position).getProfileIconId());
if (icon==null){
RequestParams requestParams = new RequestParams();
requestParams.put("region",itemsArrayList.get(position).getRegion());
requestParams.put("id",itemsArrayList.get(position).getProfileIconId());
LeagueAssistantAPI.get("leagueAssistant/icon", requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Icon saveIcon=new Icon(itemsArrayList.get(position).getProfileIconId(),responseBody);
databaseHandler.addIcon(saveIcon);
Bitmap bitmap = BitmapFactory.decodeByteArray(responseBody, 0, responseBody.length);
Drawable iconDrawable = new BitmapDrawable(bitmap);
iconView.setImageDrawable(iconDrawable);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
error.printStackTrace();
}
});
}
else{
Bitmap bitmap = BitmapFactory.decodeByteArray(icon.getIcon(), 0, icon.getIcon().length);
Drawable iconDrawable = new BitmapDrawable(bitmap);
iconView.setImageDrawable(iconDrawable);
}
summonerName.setText(itemsArrayList.get(position).getName());
region.setText(itemsArrayList.get(position).getRegion());
if (itemsArrayList.get(position).isSelected())
checkBtn.setBackgroundResource(R.drawable.checked);
else checkBtn.setBackgroundResource(R.drawable.check);
return rowView;
}
}
我有另一个在数据库Login
中添加新行的活动。我从MainActivity
转到SummonersActivity
到LoginActivity
。 LoginActivity
在数据库中插入然后返回SummonersActivity
。我将notifiyDataSetChanged()
放入onResume
SummonersActivity
。退出LoginActivity
后,列表不会自动更新,但如果我返回MainActivity
然后返回SummonersActivity
,则列表会更新。我试图将notifiyDataSetChanged()
放在不同的地方,但那不起作用。有人可以指出为什么退出ListView
后LoginActivity
没有更新?
答案 0 :(得分:1)
实际上,通过调用super(context, R.layout.summoner_list_row, databaseHandler.getAllSummoners());
将适配器绑定到适配器构造函数中的数组。您需要覆盖应访问内部适配器列表的getCount()
和getItem(int position)