请教我的这个Fragment为什么始终只显示一条数据?
public class CopyOfPersonDynamic extends DemoListFragment {
View mRootView;
private Context mContext;
private ListView mListView;
private DataAdapter mAdapter;
private Handler mHandler;
@Override
public View onCreateView(LayoutInflater mInflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mAdapter.refreshDataAdapter();
mListView.requestLayout();
mAdapter.notifyDataSetChanged();
}
};
mContext = container.getContext();
mRootView = mInflater.inflate(R.layout.tabroot, null);
mListView = (ListView) mRootView.findViewById(R.id.dynamicList);
mAdapter = new DataAdapter();
mListView.setAdapter(mAdapter);
mListView.setClickable(true);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {// 点击单行跳转至分时图页面并显示该代码的分时图
Intent it = new Intent(getActivity(), DemoActivity.class);
startActivity(it);
}
});
refreshTask();
return mRootView;
}
private void refreshTask() {
Timer timer = new Timer(false);
TimerTask task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if (GetData.getData())
mHandler.sendEmptyMessage(1);
}
};
timer.schedule(task, 1);
}
private class DataAdapter extends BaseAdapter {
ArrayList<String> mArrs = new ArrayList<String>();
@Override
public int getCount() {
if (mArrs != null)
return mArrs.size();
else
return 0;
}
public void refreshDataAdapter() {
if (mArrs != null)
mArrs.clear();
mArrs.addAll(GetData.mDatas);
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.item, null);
}
int temp = mArrs.size() - 1 - position;// 倒序
if (temp >= 0 && temp < mArrs.size()) {
setItemValue(temp, convertView);
}
return convertView;
}
private void setItemValue(final int position, View convertView) {
TextView releasetime = (TextView) convertView
.findViewById(R.id.releasetime);
releasetime.setText(mArrs.get(position));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1、
setItemValue(temp, convertView);
这里的temp是什么?
2、
refreshTask()应该在ListView.setAdapter(adapter);之后调用,会比较安全;
3、
尚不知道你的GetData.getData()会得到多少数据,所以建议你看看这个方法是否有问题,是否返回正确的数据。
4、
你的代码写的太不规范了,看着真令人头疼,变量的写法风格请统一规范一点!
你是否嵌套使用scrollview和listview?可能是因为listview高度需要重新计算