Android 列表视图刷新

发布于 2024-09-30 11:01:27 字数 1066 浏览 4 评论 0原文

我有一个 ListView,它通过数组向我显示一些数据(该数组位于另一个类中,我通过它的对象访问它)。

每当我通过上下文菜单从 ListView 中删除元素时,列表不会刷新,但该元素会从数组中删除。我怎样才能刷新列表来显示这一点?

代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泡沫很甜 2024-10-07 11:01:27

您必须通知您的 ListView 适配器数据已更改。

listViewAdapater.notifyDataSetChanged();

如果由于某种原因不起作用,并且在一些奇怪的情况下似乎没有通知,您可以通过构造函数使用更新的数组重新分配适配器。

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.

一百个冬季 2024-10-07 11:01:27

我没有 notifyDataSetChanged() 方法(Android 2.3)。

以下内容对我有用:

getListView().invalidate();

就我而言,我已经更改了整个适配器,但它仍然没有刷新,所以对我来说,上述方法是唯一有效的解决方案。

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.

寄意 2024-10-07 11:01:27

尝试 getListView()。refreshDrawableState

Try getListView().refreshDrawableState

她说她爱他 2024-10-07 11:01:27
    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;
}
}
    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;
}
}
走过海棠暮 2024-10-07 11:01:27

对我来说列表刷新: listView.invalidateViews();

它会做:

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

管理某些列表项删除或一次删除所有元素的情况。

在 API 24 中测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文