How to update a listview from the action bar button

2

I'd like to know how I can do to update a listView from a button in the action bar . Clicking on this would be updated by taking the data again from the link site. This listview takes data from the site link and does the implementation of the data.

I'm using methods: RemoteDataTask AsyncTask and onPostExecute .

Adapter Code

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
private List<WorldPopulation> worldpopulationlist = null;
private ArrayList<WorldPopulation> arraylist;

public ListViewAdapter(Context context,
                       List<WorldPopulation> worldpopulationlist) {
    this.context = context;
    this.worldpopulationlist = worldpopulationlist;
    inflater = LayoutInflater.from(context);
    this.arraylist = new ArrayList<WorldPopulation>();
    this.arraylist.addAll(worldpopulationlist);
    imageLoader = new ImageLoader(context);

}

public class ViewHolder {
    TextView rank;
    TextView country;
    TextView population;
    ImageView flag;
}

@Override
public int getCount() {
    return worldpopulationlist.size();
}

@Override
public Object getItem(int position) {
    return worldpopulationlist.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.activity_item_ofertas, null);
        // Locate the TextViews in listview_item.xml
        holder.rank = (TextView) view.findViewById(R.id.rank);
        holder.country = (TextView) view.findViewById(R.id.country);
        holder.population = (TextView) view.findViewById(R.id.population);
        // Locate the ImageView in listview_item.xml
        holder.flag = (ImageView) view.findViewById(R.id.flag);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // Set the results into TextViews
    holder.rank.setText(worldpopulationlist.get(position).getRank());
    holder.country.setText(worldpopulationlist.get(position).getCountry());
    holder.population.setText(worldpopulationlist.get(position)
            .getPopulation());
    // Set the results into ImageView
    imageLoader.DisplayImage(worldpopulationlist.get(position).getFlag(),
            holder.flag);

    return view;
  }

}
    
asked by anonymous 24.04.2015 / 05:49

2 answers

2
Assuming you have already created an xml file for the menu items and you have already determined that your Action Bar will use this file to generate the menu items, the next step is to determine what happens when each Action menu item Bar is clicked. This is done as follows:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Trata cliques nos itens da Action Bar
        switch (item.getItemId()) {
            case R.id.opcao_1_do_menu:

                //Código para o clique na opcao 1
                return true;

            case R.id.opcao_atualizar_lista:

                // Código para atualizar a lista
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

For the code to update the List View , the best approach would be to update only the Adapter (containing the data) and notify the List View to which it is associated that the data has been modified and therefore the List View needs to be updated. To do this perform the following method after inserting / removing / updating data in your Adapter :

meuAdapter.notifyDataSetChanged();

This method could stay within the method that takes the site data. For example, suppose that in your activity you have a public void pegarDados() method, which accesses the site, takes the data and receives it as a Data list. So, after receiving this list, you must add the list data in the adapter.

public void pegarDados(){

    List<Dados> listaDados = suaListaDeDadosDoSite;

    for(Dado d : listaDados){

        meuAdapter.add(Dado);
    }

    meuAdapter.notifyDataSetChanged;
}

Note that :

  • You will have to create a public void add(Dado) method in your ListAdapter.
  • If your data list received from the site has elements that already exist in the current adapter, then you will have to instantiate a new adapter within public void pegaDados() , instead of adding the elements received in the current adapter.
24.04.2015 / 21:28
1

Hello. Thanks to everyone who tried to help, I just tried the way you said it but I could not, so I decided to do it by looking for something in the code that I could use to call this action that started once the app is opened:

new RemoteDataTask().execute();

But when I looked at it, I saw the need to call it again from the refresh button I have in my ACTION BAR, and that's what I did:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // Trata cliques nos itens da Action Bar
    switch (item.getItemId()) {
        case R.id.icon_refresh:
            new RemoteDataTask().execute(); //ISSO AQUI FOI FUNDAMENTAL, chamado novamente para carregar os dados do site parse.com

            //Código para o clique na opcao 1
            default:
                return super.onOptionsItemSelected(item);
    }
}

@regmoraes. Thanks for all the tips and attempts to solve my problem. Show! Working perfectly!

    
25.04.2015 / 06:28