我的listview是放在fragment中的,网上搜了很多资料,有说要用ListFragment的,有说是Fragment缓存的。 我的adapter里面getCount打印出来是不为0的,但是始终走不到getView里面。 我没有在adapter构造方法里面传值,我是先调用 if (null == mTrackListAdapter) { mTrackListAdapter = new ProductionTrackListAdapter(getActivity()); } mListView.setAdapter(mTrackListAdapter); 然后调用 mTrackListAdapter.setData() 方法,并且在setData()里面调用notifyDataSetChanged(); 看这里说是缓存的原因http://www.eoeandroid.com/forum.php?mod=viewthread&tid=299336&extra=page%3D1&page=1 有人碰到过这种情况吗?求解答。 |
|
20分
#1 |
告诉你一个笨方法,不要调用notifidatasetchanged,而是每次创建新的adapter对象,可以解决问题
|
#2 |
回复1楼: 也没用啊。我写demo的时候在fragment里面使用listview是没问题的,到项目里面就出错了。 日了狗。 是不是缓存的问题啊 |
#3 |
你的getCount()是0不是说明List集合里面没有数据吗?
|
#4 |
回复3楼: 大哥,不为0啊。 我没有在构造方法里面传值,一开是为0的。但是后来我用setData方法传值了,而且notifyDataChange了,这时候getCount打印的是不为0 的,但是还是不会走getView方法。 |
20分
#5 |
看看fragment是不是出于pause状态。看onpause
|
#6 |
回复5楼: 我在onResume和onPause分别打印了log,只有看到onResume的log,onPause的没有 |
#7 |
我贴一下我的代码吧
这是Fragment的 package com.baosteel.moa.productiontrack; import java.util.HashMap; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ListView; import android.widget.Toast; import com.baosteel.moa.R; import com.baosteel.moa.common.BaseFragment; import com.baosteel.moa.http.BaseRequest; import com.baosteel.moa.http.WorkRequest; import com.baosteel.moa.http.bean.ProductionTrackGetContractsResponse; import com.baosteel.moa.http.listener.RequestSuccessedListener; import com.baosteel.moa.interfaces.Initialization; import com.baosteel.moa.util.FileUtil; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2; import com.handmark.pulltorefresh.library.PullToRefreshListView; /*** * 合同跟踪 * * @author Administrator * */ public class ProductionTrackFragment extends BaseFragment implements Initialization { private View mView; private Gson mGson; private ProductionTrackGetContractsResponse mContractResponse; private PullToRefreshListView mListView; private ProductionTrackListAdapter mTrackListAdapter; private int mPageNum; private static final int PAGE_SIZE = 10; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); mView = inflater.inflate(R.layout.production_track_fragment, null); return mView; } @Override public void onInitViews() { mListView = (PullToRefreshListView) mView .findViewById(R.id.production_track_listview); } @Override public void onInitListener() { mListView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "点我干哈?", 0).show(); } }); mListView.setOnRefreshListener(new OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh( PullToRefreshBase<ListView> refreshView) { requestContract(0); } @Override public void onPullUpToRefresh( PullToRefreshBase<ListView> refreshView) { requestContract(++mPageNum); } }); } @Override public void onInitData() { mGson = new Gson(); if (null == mTrackListAdapter) { mTrackListAdapter = new ProductionTrackListAdapter(getActivity()); } mListView.setAdapter(mTrackListAdapter); requestContract(0); } @Override public void onResume() { super.onResume(); v("onResume"); } @Override public void onPause() { super.onPause(); v("onPause"); } public void requestContract(int pageNum) { String content = new String(FileUtil.readText(getActivity(), "ProductionTrackGetContract_")); HashMap<String, String> params = new HashMap<String, String>(); params.put(BaseRequest.PARAMETER_POSTDATA, String.format(content, pageNum, PAGE_SIZE)); WorkRequest request = new WorkRequest(getActivity(), params, new RequestSuccessedListener() { @Override public void requestSuccessed(String result) { FileUtil.saveToDisk("getContract", result); mContractResponse = mGson.fromJson(result, ProductionTrackGetContractsResponse.class); mTrackListAdapter = new ProductionTrackListAdapter(getActivity()); mTrackListAdapter .setData(mContractResponse.blocks.r0.rows); mListView.setAdapter(mTrackListAdapter); } }); request.execute(); mPageNum = pageNum; } } 这是Adapter的 package com.baosteel.moa.productiontrack; import java.util.ArrayList; import java.util.Collections; import java.util.List; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.baosteel.moa.R; public class ProductionTrackListAdapter extends BaseAdapter { public static final int TYPE_TITLE = 0; public static final int TYPE_CONTENT = 1; private Context mContext; private List<List<String>> mData; /** 记录title位置的布尔数组 */ private List<Integer> mTitlePosition; /** 记录title的值(即日期) */ private List<String> mTitleInfo; private int mTitleAddedCount = 0; private int mCount; public ProductionTrackListAdapter(Context context, List<List<String>> rows) { this.mContext = context; this.mTitlePosition = new ArrayList<Integer>(1); this.mTitleInfo = new ArrayList<String>(1); this.mData = rows; getTitleInfo(rows); this.mCount = mData.size() + mTitleInfo.size(); } public ProductionTrackListAdapter(Context context) { this.mContext = context; this.mTitlePosition = new ArrayList<Integer>(1); this.mTitleInfo = new ArrayList<String>(1); this.mData = new ArrayList<List<String>>(1); } @Override public int getCount() { v("getCount:" + this.mCount); return this.mCount; } @Override public Object getItem(int position) { return 0; } @Override public long getItemId(int position) { return position; } @Override public int getViewTypeCount() { v("getViewTypeCount"); return 2; } @Override public int getItemViewType(int position) { v("getItemViewType"); int type; if (mTitlePosition.contains(position)) { type = TYPE_TITLE; } else { type = TYPE_CONTENT; } return type; } @Override public View getView(int position, View convertView, ViewGroup parent) { v("getView"); int type = getItemViewType(position); switch (type) { case TYPE_TITLE: if (null == convertView) { convertView = LayoutInflater.from(mContext).inflate( R.layout.production_track_listview_title, null); mTitleHolder = new TitleHolder(); mTitleHolder.titleTv = (TextView) convertView .findViewById(R.id.production_track_listview_title_tv); convertView.setTag(mTitleHolder); } else { mTitleHolder = (TitleHolder) convertView.getTag(); } mTitleHolder.titleTv.setText(mTitleInfo.get(mTitleAddedCount++)); break; case TYPE_CONTENT: if (null == convertView) { convertView = LayoutInflater.from(mContext).inflate( R.layout.production_track_listview_content, null); mContentHolder = new ContentHolder(); mContentHolder.contractTv = (TextView) convertView .findViewById(R.id.production_track_listview_content_contract); mContentHolder.amountTv = (TextView) convertView .findViewById(R.id.production_track_listview_content_amount); convertView.setTag(mContentHolder); } else { mContentHolder = (ContentHolder) convertView.getTag(); } mContentHolder.amountTv.setText(this.mData.get( position - mTitleAddedCount).get(25)); mContentHolder.contractTv.setText(this.mData.get( position - mTitleAddedCount).get(0)); break; default: break; } return convertView; } private TitleHolder mTitleHolder; private class TitleHolder { TextView titleTv; } private ContentHolder mContentHolder; private class ContentHolder { TextView contractTv; TextView amountTv; } public void setData(List<List<String>> rows) { v("setData"); Collections.sort(rows, new ProductionTrackComparable()); this.mData.clear(); this.mData.addAll(rows); this.mTitleAddedCount = 0; getTitleInfo(rows); notifyDataSetChanged(); } /** 计算不同日期的个数 */ private void getTitleInfo(List<List<String>> rows) { this.mTitleInfo.clear(); this.mTitlePosition.clear(); String tStr = ""; final List<List<String>> tRowsList = rows; for (int i = 0; i < tRowsList.size(); i++) { List<String> tList = tRowsList.get(i); String compareStr = tList.get(7); if (!tStr.equals(compareStr)) { tStr = compareStr; this.mTitleInfo.add(compareStr); this.mTitlePosition.add(i); } } this.mCount = mData.size() + mTitleInfo.size(); } public void v(String msg) { Log.v("adapter", msg); } } |
#8 |
debug可以解决你的问题
|
#9 |
你的基类呢?
|
#10 |
回复8楼: 看老夫这么规范简洁高效的代码,老夫是个不会debug的人? |
#11 |
回复9楼: package com.baosteel.moa.common; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.baosteel.moa.interfaces.Initialization; public class BaseFragment extends Fragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (this instanceof Initialization) { Initialization init = (Initialization) this; init.onInitViews(); init.onInitListener(); init.onInitData(); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { BaseActivity baseActivity = (BaseActivity) getActivity(); baseActivity.initHeader(); return super.onCreateView(inflater, container, savedInstanceState); } public void v(String msg) { Log.v(BaseFragment.class.getCanonicalName(), msg); } } package com.baosteel.moa.interfaces; /** 如果需要对布局控件等初始化,让对应的Activity或Fragment实现该接口(为了代码的规范化) */ public interface Initialization { /** 执行findviewbyid等操作 */ public void onInitViews(); /** 绑定监听器 */ public void onInitListener(); /** 请求网络数据等 */ public void onInitData(); } |
#12 |
解决了,布局里面listview的高度设成了wrap_content,因为第一次设置adapter的时候getCount返回为0,所以listview的高度也是0,所以第二次adapter设置数据的时候就不会调用getView,把高度设成match_parent就好了。
以上只是我的猜测。 |