0

我有一个 ListView 和一个自定义适配器。当用户从微调器更改排序类型时,我想用新项目重绘列表。问题是 ListView 只有在我在设备上与它交互后才会重新绘制(例如开始滚动)。

if (update) {
                mOfferList = (ArrayList<PositionSearchItem>) psr
                            .getPositionSearchItem();

                mAdapter.clear();
                mAdapter.addAll(mOfferList);
                mAdapter.notifydataSetChanged()
                mList.invalidate();
                mList.invalidateViews();

}

我还尝试将我的适配器的新实例完全重置到列表中,它给出了相同的结果,只是列表首先变为空白并出现在用户交互中。

我正在使用 HoloEveryWhere,也许这是一个重要信息。

编辑:这是更多代码

@EFragment(R.layout.list_fragment_results)
public class ResultsListFragment extends Fragment {

    @ViewById(R.id.search_results_list)
    ListView mList;
    @ViewById(R.id.search_result_list_spinner)
    Spinner mSpinner;
    MainActivity mActivity;
    private String mTypedText;
    private List<String> mSortType;
    private ResultsListFragment mFrag;
    private Bundle mOffersPageBundle;
    private SearchResultsListAdapter mAdapter;
    private ArrayList<PositionSearchItem> mOfferList;

    @AfterViews
    public void afterViews() {
        mFrag = this;
        mActivity = (MainActivity) getActivity();
        mSortType = new ArrayList<String>();
        mSortType.add(SearchResultSortType.SCORE_DESCENDING);
        mOfferList = new ArrayList<PositionSearchItem>();

        mOffersPageBundle = getArguments();
        PositionSearchResponse psr = mOffersPageBundle.getParcelable("offers");
        mTypedText = mOffersPageBundle.getString("typedText");

        mOfferList = (ArrayList<PositionSearchItem>) psr.getPositionSearchItem();

        mAdapter = new SearchResultsListAdapter(this,
                android.R.layout.simple_list_item_1, mOfferList, mTypedText,
                mSortType, mActivity.getLat(), mActivity.getLon());
        mList.setAdapter(mAdapter);

        this.setListeners();
    }

    private void setListeners() {

        mSpinner.setOnItemSelectedListener(new org.holoeverywhere.widget.AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(
                    org.holoeverywhere.widget.AdapterView<?> parent, View view,
                    int pos, long id) {
                Boolean update = false;
                PositionSearchResponse psr = null;
                switch (pos) {
                case 0:
                    if (mSortType.get(0) != SearchResultSortType.SCORE_DESCENDING) {
                        mSortType.clear();
                        mSortType.add(SearchResultSortType.SCORE_DESCENDING);
                        psr = processSearch(mTypedText, mSortType, 0, mActivity);
                        update = true;
                    }
                    break;
                case 1:
                    if (mSortType.get(0) != SearchResultSortType.DATE) {
                        mSortType.clear();
                        mSortType.add(SearchResultSortType.DATE);
                        psr = processSearch(mTypedText, mSortType, 0, mActivity);
                        update = true;
                    }
                    break;

                default:
                    break;
                }

                if (update) {
                    mOffersPageBundle.putParcelable("offers", psr);
                    mOfferList = (ArrayList<PositionSearchItem>) psr
                            .getPositionSearchItem();

                    mAdapter.mIdMap.clear();
                    for (int i = 0; i < mOfferList.size(); ++i) {
                        mAdapter.mIdMap.put(mOfferList.get(i), i);
                    }

                    mOfferList = (ArrayList<PositionSearchItem>) psr
                            .getPositionSearchItem();

                    mAdapter.clear();
                    mAdapter.addAll(mOfferList);
                    mAdapter.notifydataSetChanged()
                    mList.invalidate();
                    mList.invalidateViews();
                }
            }

            @Override
            public void onNothingSelected(
                    org.holoeverywhere.widget.AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });
    }
}

编辑:自定义适配器的 getView 方法

@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    this.v = convertView;
    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    PositionSearchItem psr = getItem(pos);

    v = vi.inflate(R.layout.result_list_item, parent, false);
    v.setTag(pos);
    // Inject text into view
    ((TextView) v.findViewById(R.id.result_title_textview))
            .setText(getItem(pos).getTitle());

    // Disable hardware acceleration for the view (it brakes dotted lines on
    // some devices)
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    // reload list if scrolled to bottom
    int page = 0;
    if (pos > mLastViewed && pos == getCount() - 1) {
        mLastViewed = pos;
        ResultsListFragment frag = new ResultsListFragment_();
        PositionSearchResponse newSearch = frag.processSearch(mTypedText, mSortType, mPage,
                mActivity);
        mPage = mPage + 12;

        if(newSearch != null && newSearch.getPositionSearchItem() != null && newSearch.getPositionSearchItem().size() > 0){
            for (int i = 0; i < newSearch.getPositionSearchItem().size(); ++i) {
                mIdMap.put(newSearch.getPositionSearchItem().get(i), i);
            }
            this.addAll(newSearch.getPositionSearchItem());
        }

    }
    return v;
}
4

3 回答 3

0

利用

if (update) {

mAdapter.NotifyDataSetChanged();
}
于 2014-01-13T11:49:23.523 回答
0

这可能会解决您的问题。

尝试再次设置adatpter。将以下代码放在您编写代码以更新列表视图的所有位置

mAdapter =  new YourAdapter();
mList.setAdapet(mAdapter)
于 2014-01-13T11:53:37.707 回答
0

我希望您已经使用适配器和模型创建了自定义列表视图。

现在,每次需要动态更新列表视图时,都需要清除之前创建的列表视图。然后在您需要获取所有新值然后在自定义列表视图中更新相同的值之后。

希望能帮助到你!

于 2014-01-13T11:53:54.620 回答