Android 如何在更新listview时添加按钮?
我正在开发一个从GCM获取数据的应用程序,在此之后,我在listview中显示数据,当从GCM获取新数据时,我将这些新数据添加到listview中,并使用此代码Android 如何在更新listview时添加按钮?,android,listview,button,Android,Listview,Button,我正在开发一个从GCM获取数据的应用程序,在此之后,我在listview中显示数据,当从GCM获取新数据时,我将这些新数据添加到listview中,并使用此代码 public void updateListView2(List<FeedItem> newItems) { for(FeedItem item : newItems) { //Toast.makeText(this, "Title : "+item.getTitle(), Toast.LEN
public void updateListView2(List<FeedItem> newItems) {
for(FeedItem item : newItems)
{
//Toast.makeText(this, "Title : "+item.getTitle(), Toast.LENGTH_LONG).show();
// this to add the new items at the top of the list
listAdapter.insert(item, 0);
}
// to retain the position of the listview after updated
int lastViewIndex = listView.getFirstVisiblePosition();
View view = listView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop() ;
listView.setSelectionFromTop(lastViewIndex, top);
listAdapter.notifyDataSetChanged();
//this to add the button programitcally like "New Stories"
Button updateBt = new Button(this);
updateBt.setText("Updated");
feedLayout.addView(updateBt);
}
public void updateListView2(列出新项){
对于(FeedItem:newItems)
{
//Toast.makeText(这个,“Title:+item.getTitle(),Toast.LENGTH\u LONG.show();
//此选项用于在列表顶部添加新项目
listAdapter.insert(项,0);
}
//更新后保留listview位置的步骤
int lastViewIndex=listView.getFirstVisiblePosition();
View=listView.getChildAt(0);
int top=(view==null)?0:view.getTop();
setSelectionFromTop(lastViewIndex,top);
listAdapter.notifyDataSetChanged();
//这可以像“新故事”一样以编程方式添加按钮
按钮更新bt=新按钮(此按钮);
updateBt.setText(“已更新”);
feedLayout.addView(updateBt);
}
但是当我尝试添加像“新故事”这样的按钮时,我失败了,什么都看不到!!有人能帮我吗 我会玩可视性(可视和消失)。当您获得信息时,不必添加按钮,只需使其可见,并在单击按钮(或任何您想要的事件)后使其不可见即可
这可能会使您更容易使用视图的层次结构,因为您可以在xml布局中定义按钮(默认情况下,在顶部或需要的位置取消可见性),并以友好方式更改其属性。最好的方法是添加该按钮,并将其放置在xml布局文件中任何需要的位置,然后进行设置
android:visibility="gone"
或
当您需要显示按钮时,可以从java代码中显示它,如下所示
myButton.setVisibility(View.VISIBLE);
按照此操作,您将不会遇到任何问题。添加按钮后是否调用了listView的notifyDatasetChanged()方法?没有,但我没有尝试在列表中添加按钮,我只需要通知用户listvew已更新,当单击按钮时,他会进入listView顶部,如下图所示:)检查此链接。。。
myButton.setVisibility(View.VISIBLE);