16

I have one ListView which is showing me some data through an array (which is in another class and I'm accessing it through it's object).

Whenever I delete an element from the ListView through context menu, the list is not refreshing but the element is deleted from the array. How can I refresh the list to show this?

Code:

     public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    if (v.getId()==R.id.mainListView) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        post=info.position;
      menu.setHeaderTitle(stocks[info.position]);
        String[] menuItems = stt;
        for (int i = 0; i<menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
            }
    }
  }

     @Override
      public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int menuItemIndex = item.getItemId();
        String[] menuItems = stt;
        String menuItemName = menuItems[menuItemIndex];
        listItemName = stocks[info.position];
        stockname.remove(post-1);

    return true;
  }
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
vivek_Android
  • 1,697
  • 4
  • 26
  • 43

5 Answers5

41

You have to notify your ListView adapter that the data has changed.

listViewAdapater.notifyDataSetChanged();

If that for some reason doesn't work and there are some wierd situations where it seems like it wasn't notifying, you can just reassign your adapter via the constructor with the updated array.

Buffalo
  • 3,861
  • 8
  • 44
  • 69
blindstuff
  • 18,298
  • 10
  • 47
  • 48
  • where i need to put this line? – vivek_Android Nov 03 '10 at 15:51
  • 1
    right after stockname.remove(post-1); listViewAdapter is just an example, you have to substitute that with what ever your adapter is called. If you cant access it, just make it a global variable for that class. – blindstuff Nov 03 '10 at 15:52
  • 4
    Try reinitializing the adapter, just as you did when you created it. – blindstuff Nov 03 '10 at 16:00
  • 3
    Hi brothers! I know the god solutin should be the notifyDataSetChaned(), but I didn`t worked for me. I was made a list with up and down arrow for each element. If the user push the button two item shall swap with each other. So the method what you are suggesting isn`t works for me, I had to reset the adapter: lw.setAdapter(mAdapter) . Thx: Karoly from Hungary – narancs Aug 02 '11 at 19:26
  • @ blindstuff but if i reinitializing the adapter it will refresh the whole listView again, i tried using listItem.set(editPosition, imageFileName); m_Adapter = new ListViewAdapter(ListViewActivity.this, listItem); setListAdapter(m_Adapter); m_Adapter.notifyDataSetChanged(); – AndroidDev Jun 19 '12 at 11:56
  • For me it was helpful to create a new Cursor, add it with changeCursor() to the adapter and then call notifyDataSetChanged() – Yashima Aug 06 '12 at 11:50
  • 1
    @blindstuff reinitializing the adapter costs a lot of resources and garbage collection, would not recommend that – Akshat Agarwal Nov 07 '13 at 17:58
  • @AkshatAgarwal in 2010 we were using Android 2.2, it sometimes didnt work without reinitializing. I doubt this is still an issue in 4+. Of course it implies a lot of resources, but it worked. – blindstuff Nov 08 '13 at 03:59
4

I have no notifyDataSetChanged() method (Android 2.3).

The following worked for me:

getListView().invalidate();

In my case I have changed the whole adapter and it still didn't refresh, so for me the method described above is the only working solution.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
0

Try getListView().refreshDrawableState

Edwin Evans
  • 2,726
  • 5
  • 34
  • 47
0
    private static ArrayList<Radio> m_streamsList;

    final ListView listView = (ListView) rootView.findViewById(R.id.stream_listRS);

    //my class where i insert data into my array
    m_myDBHelp = new DBHelper();
    m_streamsList = m_myDBHelp.selectStreamsList();

    // my list adapter - this is a class created by me
    RadioAdapter adapt = new RadioAdapter(context, m_streamsList);
    listView.setAdapter(adapt);

    ////////////------------- so . when i inserted a row in db, i need to refresh the list
    m_myDBHelp.insertStreamIntoDB(String name, String url);
    /// in my case method invalidate works fine
    listView.invalidate();
    m_streamsList = null;
    m_streamsList = m_myDBHelp.selectStreamsList();

    //you need to set adapter again
    RadioAdapter adapt2 = new RadioAdapter(context, m_streamsList);
    listView.setAdapter(adapt2);






import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.npopa.musicplayern.R;
import com.example.npopa.musicplayern.Radio;

import java.util.ArrayList;

public class RadioAdapter extends BaseAdapter{
private ArrayList<Radio> m_streams;
private LayoutInflater m_stramInf;

public RadioAdapter(Context c, ArrayList<Radio> theStreams){
    m_streams = theStreams;
    m_stramInf = LayoutInflater.from(c);
}

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

@Override
public Object getItem(int arg0) {
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //map to radio_s layout
    LinearLayout streamLay = (LinearLayout) m_stramInf.inflate(R.layout.all_songs_t, parent, false);

    TextView name = (TextView) streamLay.findViewById(R.id.song_title);
    TextView url = (TextView) streamLay.findViewById(R.id.song_artist);
    ImageView imgALb = (ImageView) streamLay.findViewById(R.id.albumicon);

    //get all_songs_t using position
    Radio currStream = m_streams.get(position);
    name.setText(currStream.getName());
    url.setText(currStream.getUrl());
    imgALb.setImageResource(R.drawable.radio_stream);

    //set position as tag
    streamLay.setTag(position);
    return streamLay;
}

}

public class Radio {
private int id;
private String name;
private String url;

public Radio(int id, String name, String url) {
    this.id = id;
    this.name = name;
    this.url = url;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}
}
Nicu P
  • 253
  • 2
  • 6
  • You need to invalidate() your list. in my case i invalidate() the list, initialize list with null elements, and then reinitialize my list – Nicu P Jul 18 '16 at 21:14
0

List refreshing for me: listView.invalidateViews();

It'll do:

mDataChanged = true;
rememberSyncState();
requestLayout();
invalidate();

The case as to manage some list item removal or removal all element at once.

Tested in API 24.

Osify
  • 2,253
  • 25
  • 42