我正在使用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.xitiz.xitizmobile E / AndroidRuntime:致命异常:主要 处理:com.xitiz.xitizmobile,PID:6586 显示java.lang.NullPointerException at com.xitiz.xitizmobile.PostUserItineraryActivity $ 7.onClick(PostUserItineraryActivity.java:327) 在android.view.View.performClick(View.java:4444) 在android.view.View $ PerformClick.run(View.java:18440) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5052) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:796) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:0)
执行此操作的简单方法是制作PostItinerarySearchCityAdapter extends RealmBaseAdapter<PostUserItinerary>
,直接使用RealmResults<PostUserItinerary>
初始化,然后将您直接拨打的所有电话移至adapter.notifyDataSetChanged();
。
您可以在Realm的示例中看到示例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());