微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – ListFragment不显示当前时间的变化

所有!我是 Android应用程序的新手.现在我有一些问题.我无法立即看到更改列表的结果(在我的ListFragment中更新listView之后).
例如,我调用方法addNewItem,我没有在屏幕上看到任何更改.但如果我触摸ListFragment,我会看到所有的变化.

ListFragment:

public class PointsListFragment extends ListFragment {

    PlaceItemsAdapter adapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new PlaceItemsAdapter(
                getActivity(),R.layout.place_list_item,new ArrayList<PlaceItem>());
        setlistadapter(adapter);
        adapter.notifyDataSetChanged();
    }

   public void addNewItem(int id,String address) {
     adapter.items.add(new PlaceItem(id,address));
     adapter.notifyDataSetChanged();
    }


    private class PlaceItemsAdapter extends ArrayAdapter<PlaceItem> {

        private final int viewResourceId;
        final public ArrayList<PlaceItem> items;

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

        @Nullable
        @Override
        public PlaceItem getItem(int position) {
            return items.get(position);
        }

        public PlaceItemsAdapter(Context context,int viewResourceId,ArrayList<PlaceItem> items) {
            super(context,viewResourceId,items);
            this.items = items;
            this.viewResourceId = viewResourceId;

        }

        @Override
        public View getView(int position,View convertView,ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(viewResourceId,null);
            }
            PlaceItem placeItem = getItem(position);
            TextView placeIdTextView = (TextView) v.findViewById(R.id.placeId);
            placeIdTextView.setText(String.valueOf(placeItem.getId()));
            TextView placeAddressView = (TextView) v.findViewById(R.id.placeAddress);
            placeAddressView.setText(placeItem.getAddress());
            if (selectedValue == position) {
                v.setBackgroundResource(R.drawable.active_item);
            }
            return v;
        }

    }
}

我的活动xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"[enter image description here][1]
        android:padding="2dp">
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@color/forBorder"
            android:orientation="horizontal">

            <Spinner
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/spinner"
                android:layout_weight="1" />

            <TextView
                android:text="проложить маршрут"
                android:textColor="@android:color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:drawable/ic_media_ff" />

        </LinearLayout>

        <fragment
            class="com.restfulrobot.cdcapplication.PointsListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/list_frag" />
    </LinearLayout>

图像示例

解决方法

谢谢你们!我找到了答案!我从另一个调用方法(没有Activity或Fragment).现在我也从另一个调用方法,但我通过Handler来做.它现在真的很好用!
像这样的东西:

Message msg = mainActivityHandler.obtainMessage(MainActivity.NEW_MESSAGE,someObjectToAdd);
 mainActivityHandler.sendMessage(msg);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐