我正在使用ListFragment
类似的布局,但没有遇到这个问题。尝试使用您自己Adapter
的列表项。像这样的东西
public class MyAdapter extends BaseAdapter {
@SuppressWarnings("unused")
private static final String TAG = MyAdapter.class.getSimpleName();
private List<String> _items;
private LayoutInflater inflater;
public MyAdapter(List<String> items, Context context) {
_items = items;
inflater = LayoutInflater.from(context);
}
public int getCount() {
return (_items != null) ? _items.size() : 0;
}
public Object getItem(int position) {
return (_items != null) ? _items.get(position) : null;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.your_item_layout, null);
}
((TextView)convertView.findViewById(R.id.itemText)).setText(_items.get(position));
return convertView;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public synchronized void refresAdapter(List<String> items) {
_items.clear();
_items.addAll(items);
notifyDataSetChanged();
}
}
- - 编辑 - -
这是我的代码ListFragment
public class HomeListFragment extends ListFragment implements DataListener {
private MyAdapter _adapter;
private Context _ctx;
public HomeListFragment(){}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
_ctx = getActivity();
initialize();
}
private void initialize() {
_adapter = new MyAdapter(new List<String>(), _ctx);
setListAdapter(_adapter);
final SomeServiceHandler s = new SomeServiceHandler(_ctx, HomeListFragment.this);
s.request(RequestType.GETSOMEDATA, "");
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//do stuff
}
/**
* Listener method that is invoked by SomeServiceHandler
*/
public void onResponseRecieved(List<String> responseList) {
_adapter.refresAdapter(responseList);
}
}
我唯一能看到的与你不同的是,我在通过调用SomeServiceHandler
类再次创建 Activity 之后刷新我的适配器,然后使用onResponseRecieved
方法中的响应刷新。