从本地数据库获取记录并在Listview中显示IllegalStateException:

时间:2016-04-19 04:31:17

标签: java android database android-arrayadapter

从一个表中获取ID并与另一个表匹配并从该表中获取记录。有时我在列表中获取记录但是当我想在list illegalexception中插入另一个未通知listview等的记录时。

private void getAllRecords() {

    String cmpId = "";
 //fetching ids from this table one by one and passing id to another table
    Cursor preAppointmentRecord = JasperDatabase.getInstance(PreAppointmentsActivity.this).getPreAppointmentRecord();
    preAppointmentRecord.moveToFirst();
    if (preAppointmentRecord.getCount() != 0) {
        while (!preAppointmentRecord.isAfterLast()) {
            cmpId = preAppointmentRecord.getString(1);
            getAllCompanyName(cmpId);
            preAppointmentRecord.moveToNext();
        }

        preAppointmentRecord.close();

    }
}
private void getAllCompanyName(String companyId) {
    //fetching company names with matched ids
    Cursor companyNameCursor =    JasperDatabase.getInstance(PreAppointmentsActivity.this).getPreAppointmentCompanies(companyId);
    companyNameCursor.moveToFirst();
    if (companyNameCursor.getCount() != 0) {
        while (!companyNameCursor.isAfterLast()) {
//getting second string from database                
recordsAr.add(new PreAppointmentRecord(companyNameCursor.getString(2)));
            companyNameCursor.moveToNext();
        }
        companyNameCursor.close();
    }
}

用于调用getAllRecords()函数的AsyncTask类:

 protected Void doInBackground(String[] paramArrayOfString) {
        getAllRecords();
        return null;
    }

    protected void onPostExecute(Void paramVoid) {
        super.onPostExecute(paramVoid);
        if (mProgressDialog.isShowing())
            mProgressDialog.dismiss();
   //Notifying to listview for changing view
            customRecordsAdapter.notifyDataSetChanged();
    }

客户适配器:

 @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final int k = 0 ;
       //Using for getting all record storing in recordArray
        final PreAppointmentRecord preRecord = recordsAr.get(i);

Logcat

FATAL EXCEPTION: main Process: com.jmd.jasper, PID: 11265
                                                            java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131558625, class android.widget.ListView) with Adapter(class com.jmd.jasper.activities.PreAppointmentsActivity$CustomRecordsAdapter)]
                                                                at android.widget.ListView.layoutChildren(ListView.java:1566)
                                                                at android.widget.AbsListView.onLayout(AbsListView.java:2564)
                                                                at android.view.View.layout(View.java:15601)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:4881)
                                                                at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
                                                                at android.view.View.layout(View.java:15601)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:4881)
                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                at android.view.View.layout(View.java:15601)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:4881)
                                                                at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
                                                                at android.view.View.layout(View.java:15601)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:4881)
                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                at android.view.View.layout(View.java:15601)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:4881)
                                                                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
                                                                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
                                                                at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
                                                                at android.view.View.layout(View.java:15601)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:4881)
                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)

客户适配器:

    private class CustomRecordsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return recordsAr.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            final int k = 0 ;

            final PreAppointmentRecord preRecord = recordsAr.get(i);
//            final PreAppointmentCustomer1 preRecord1 = recordsAr1.get(k);


            view = View.inflate(PreAppointmentsActivity.this, R.layout.custom_record_list_preappointment, null);
            TextView companyNameTextView = (TextView) view.findViewById(R.id.companyNameTextView);


            companyNameTextView.setText(preRecord.CompanyName);
    //            nameTextView.setText(preRecord1.CustomerId);
     return view;
        }
    }

1 个答案:

答案 0 :(得分:0)

我有同样的问题,但我使用方法

修复了它

<强> requestLayout();

来自ListView

编辑

myListView.requestLayout();