gpt4 book ai didi

android - GridView CustomAdapter notifyDataSetChanged 方法问题

转载 作者:行者123 更新时间:2023-11-29 14:37:17 24 4
gpt4 key购买 nike

我有一个 Fragment,使用 CustomAdapter(extending BaseAdapter) 加载了 GridView。数据是从服务器检索的,因此,一旦下载完成,我将尝试在适配器上调用 notifyDataSetChanged。但是,GridView 不会在下载后加载内容。

以下是我的一些代码 fragment :

GridView gridview;
CustomAdapter mCustomAdapter;
List<Item> listItems = new ArrayList<Item>();

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_main, container, false);
....
....

mCustomAdapter = new CustomAdapter(activity, listItems);
gridview = (GridView) view.findViewById(R.id.gridview);
gridview.setAdapter(mCustomAdapter);

// This is the call for retrieving data from Server
checkRefreshTimeAndGetData();

return view;

}

获取数据后,我在下面的方法中调用 notifyDataSetChanged():

public void checkRefreshTimeAndGetData(){
....
....

listItems.clear();

if (!mMainTableDbAdapter.isTableEmpty(
prefs.getInt(Const.Prefs.MAIN_INDEX, DEFAULT_VALUE),
prefs.getInt(Const.Prefs.SUB_INDEX, DEFAULT_VALUE))){
listItems = mMainTableDbAdapter.getTitles(
prefs.getInt(Const.Prefs.MAIN_INDEX, DEFAULT_VALUE),
prefs.getInt(Const.Prefs.SUB_INDEX, DEFAULT_VALUE));
}

// At this point, i checked if listItems has values,
// and it has 100 items in it. So, its not empty.

mCustomAdapter.notifyDataSetChanged();
}

自定义适配器:

public class CustomAdapter extends BaseAdapter {

private List<Item> items;
private LayoutInflater vi;
private List<Boolean> selectedState = new ArrayList<Boolean>();
String imagepath = "";

int curPos = 0;

public CustomAdapter(Context context, List<Item> items) {
this.items = items;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

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

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

@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {

....
....

}
}

我做错了什么?如果需要任何其他数据,请告诉我。

最佳答案

这是一条出路

在你的 Adapter 中制作 refresh(..)

 public void refresh(List<Item> items)
{
this.items = items;
notifyDataSetChanged();
}

像这样使用它

 listItems.clear();
mCustomAdapter.refresh(listItems); //pass update list

关于android - GridView CustomAdapter notifyDataSetChanged 方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29796252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com