2010-11-03 5 views
15

Tôi có một ListView hiển thị cho tôi một số dữ liệu thông qua một mảng (trong lớp khác và tôi đang truy cập nó thông qua đối tượng của nó).Xem danh sách Android làm mới

Bất cứ khi nào tôi xóa một phần tử từ ListView thông qua menu ngữ cảnh, danh sách không làm mới nhưng phần tử bị xóa khỏi mảng. Làm thế nào tôi có thể làm mới danh sách để hiển thị điều này?

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

Trả lời

38

Bạn phải thông báo cho bộ chuyển đổi ListView của bạn rằng các dữ liệu đã thay đổi.

listViewAdapater.notifyDataSetChanged(); 

Nếu lý do nào đó không hoạt động và có một số tình huống khó xử mà nó không được thông báo, bạn có thể chỉ định lại bộ điều hợp của mình thông qua hàm tạo với mảng được cập nhật.

+0

nơi tôi cần phải đặt dòng này? –

+1

ngay sau khi stockname.remove (post-1); listViewAdapter chỉ là một ví dụ, bạn phải thay thế bằng bộ điều hợp của bạn. Nếu bạn không thể truy cập nó, chỉ cần biến nó thành biến toàn cầu cho lớp đó. – blindstuff

+4

Thử khởi động lại bộ điều hợp, giống như khi bạn tạo nó. – blindstuff

4

Tôi không có phương thức notifyDataSetChanged() (Android 2.3).

Sau đây làm việc cho tôi:

getListView().invalidate(); 

Trong trường hợp của tôi, tôi đã thay đổi toàn bộ adapter và nó vẫn không làm mới, vì vậy cho tôi phương pháp mô tả ở trên là giải pháp làm việc mà thôi.

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

Bạn cần vô hiệu hóa() danh sách của bạn. trong trường hợp của tôi i invalidate() danh sách, khởi tạo danh sách với các phần tử null, và sau đó reinitialize danh sách của tôi –

0

Danh sách làm mới cho tôi: listView.invalidateViews();

Nó sẽ thực hiện:

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

trường hợp

Toán tử as để quản lý một số loại bỏ mục danh sách hoặc loại bỏ tất cả các yếu tố một lần.

Đã kiểm tra trong API 24.