1

Getting ids from one table and matching with another table and getting record from that table.Some time i get records in list but when i want to insert one more record in list illegalexception that is not notified to listview etc.

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();
    }
}

AsyncTask class for calling to getAllRecords() function :

 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();
    }

Customer adapter:

 @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)

Customer Adapter :

    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;
        }
    }
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
Ishika Sharma
  • 55
  • 1
  • 7

1 Answers1

0

I had the same problem, but I fixed it using the method

requestLayout();

from the class ListView

Edit

myListView.requestLayout();
Sonu Kumar
  • 969
  • 1
  • 11
  • 36