How to update a listview from the bar action button

Asked

Viewed 850 times

2

I would like to know how I can update a listView from a button on action bar. By clicking this would be updated by taking the data again from the site https://parse.com. This listview picks up website data https://parse.com and implement the data.

I’m using the methods: RemoteDataTask AsyncTask and the 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;
  }

}
  • 1

    Is andorid certain?

  • @Fernando don’t know if it’s Android...

  • Opa @Jorgeb., ListView, ActionBar, AsyncTask and onPostExecute, I don’t know if other technologies have "components" with the same name? But we can undo the editing in case of doubt, and wait for the AP to pronounce.

  • 1

    @Fernando Deixa ficar, mal will not do. I also put the tag but then I took it. Leave it like this and then see.

  • Eh Android yes. That was the question?

  • @Jorge B. Eh Android yes what I’m using, and I’m having this doubt to update my listview from a boot in the action bar.

  • It was only to edit your question with the Android tag, which is already. But I can not answer your question.

  • @Jorge B. I understood sorry. Could I update Activity from the button in the action bar? Pq tbm would help.

Show 3 more comments

2 answers

2

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

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

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

meuAdapter.notifyDataSetChanged();

This method could be within the method that takes the data from the site. For example, suppose your Activity has a method public void pegarDados(), that accesses the site, takes the data and receives it as a Data list. So, after receiving this list, you should add the data from the list in Adapter.

public void pegarDados(){

    List<Dados> listaDados = suaListaDeDadosDoSite;

    for(Dado d : listaDados){

        meuAdapter.add(Dado);
    }

    meuAdapter.notifyDataSetChanged;
}

Realize that:

  • You will have to create a method public void add(Dado) in your Listadapter.
  • If your data list received from the site has elements that already exist on the current Adapter, then you will need to instantiate a new Adapter within public void pegaDados(), instead of adding the received elements to the current Adapter.
  • Hello friend thanks for the reply, I edited the first code using only one case, because I have only one button in Actionbar, but this second part of the meuAdapter.notifyDataSetChanged();. I could not identify where I can put it, in question I put the information of my Class Listviewadapter. Where I could enter this code?

  • 1

    I will edit the answer to answer better :)

1

Hello. Thanks to all who tried to help, see just tried the way you said but could not, then I decided to do alone searching the code something I could use to call this action that started as soon as 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 on my ACTION BAR, and that’s what I also 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.