user2907139
user2907139

Reputation: 97

ListView is not updating after notifydataSetChanged()

I have problems with notifyDataSetChanged(). Here is the class that handles 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();
  }


}

This is the custom adapter i made

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;
  }
}

I have another activity that adds new row in the database Login. I go from MainActivity to SummonersActivity to LoginActivity. LoginActivity insert in database then returns to SummonersActivity. I put notifiyDataSetChanged() in the onResume SummonersActivity. The list doesnt automaticaly updates after I exit LoginActivity but if I go back to MainActivity then back to SummonersActivity the list is updated. I tried to put notifiyDataSetChanged() in different places but that didn't work. Could someone point why is the ListView is not updating after I exit LoginActivity ?

Upvotes: 0

Views: 334

Answers (1)

Buckstabue
Buckstabue

Reputation: 303

In fact, you bind adapter to an array in the adapter constructor by calling super(context, R.layout.summoner_list_row, databaseHandler.getAllSummoners());. You need to override getCount() and getItem(int position) which should access your internal adapter list

Upvotes: 1

Related Questions