代码之家  ›  专栏  ›  技术社区  ›  Deepa

在使用领域删除ListView的数据后,如何在scrollView中刷新ListView?

  •  0
  • Deepa  · 技术社区  · 7 年前

    我正在使用ScrollView中的ListView。每当我对ListView数据执行删除操作时,都会删除数据,但问题是在删除数据之后,不幸的是,应用程序已经停止。我使用领域来存储数据和删除领域中的数据。 这是我的密码。

    listViewSearchCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                PostUserItinerary item = (PostUserItinerary) listViewSearchCity.getItemAtPosition(i);
                view.setBackgroundColor(Color.parseColor("#a9a9a9"));
                // change1();
                h=item.getSearchCity();
                onclick();
                // onitemclick();
    
            }
        });
    public void onclick()
    {
    
        imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //PostItinerarySearchCityAdapter adapter = new PostItinerarySearchCityAdapter(PostUserItineraryActivity.this,helper.justRefresh());
                String type =h ;
                final RealmResults<PostUserItinerary> students = realm.where(PostUserItinerary.class).findAll();
    
                PostUserItinerary postUserItinerary = students.where().equalTo("searchCity", type).findFirst();
    
                if (postUserItinerary != null) {
    
                    if (!realm.isInTransaction()) {
                        realm.beginTransaction();
                    }
    
                    postUserItinerary.deleteFromRealm();
                    realm.commitTransaction();
                    adapter.notifyDataSetChanged();
                   // listViewSearchCity.setAdapter(adapter);
    
                }
            }
            //final int position2 = holder.getAdapterPosition();
    
    
    
        });
    
    }
    

    这是我的日志秀。

    10-17 10:51:09.717 6586-6586/com。西提兹。xitizmobile E/AndroidRuntime:致命异常:main Java语言lang.NullPointerException 在com。西提兹。xitizmobile。postseritineraryactivity$7。onClick(postseritineraryactivity.java:327) 在安卓。看法看法performClick(视图.java:4444) 在安卓。看法查看$PerformClick。运行(View.java:18440) 在安卓。操作系统。处理程序。handleCallback(Handler.java:733) 在安卓。操作系统。处理程序。dispatchMessage(Handler.java:95) 在安卓。操作系统。活套。循环(Looper.java:136) 在安卓。应用程序。活动线程。main(ActivityThread.java:5052) 在java。郎。反思。方法invokenactive(本机方法) 在java。郎。反思。方法调用(方法.java:515) 在com。安卓内部的操作系统。ZygoteInit$MethodandArgscaler。运行(ZygoteInit.java:796) 在com。安卓内部的操作系统。合子岩。main(ZygoteInit.java:612) 在dalvik。系统国家艺术。主(本机方法)

    1 回复  |  直到 7 年前
        1
  •  0
  •   EpicPandaForce    7 年前

    做这件事的简单方法是 PostItinerarySearchCityAdapter extends RealmBaseAdapter<PostUserItinerary> ,使用 RealmResults<PostUserItinerary> 直接,然后删除您直接拨打的任何电话 adapter.notifyDataSetChanged(); .

    Activity Adapter 在王国的样本中。

    class MyListAdapter extends RealmBaseAdapter<PostUserItinerary> implements ListAdapter {
    
        private static class ViewHolder {
            TextView countText;
            CheckBox deleteCheckBox;
        }
    
        MyListAdapter(OrderedRealmCollection<Counter> realmResults) {
            super(realmResults);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.row, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.countText = (TextView) convertView.findViewById(R.id.textview);
                viewHolder.deleteCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
    
            final PostUserItinerary item = getItem(position);
            ...
            return convertView;
        }
    }
    

    public class ListViewExampleActivity extends AppCompatActivity {
    
        private Realm realm;
        private MyListAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_listview);
            realm = Realm.getDefaultInstance();
    
            // RealmResults are "live" views, that are automatically kept up to date, even when changes happen
            // on a background thread. The RealmBaseAdapter will automatically keep track of changes and will
            // automatically refresh when a change is detected.
            adapter = new MyListAdapter(realm.where(PostUserItinerary.class).findAllAsync());