Android - Adding Columns to Matrix Cursor and Showing Column Count Toast causing Crash
Asked Answered
U

1

0

Im using a matrix cursor to add rows to cursor.

Defined columns in the MainActivity on Create

//In protected void onCreate

String[] columns = new String[] { "dealername", "product","type","description","location","sublocation","address","phone1","phone2","auth","brands","lat","lon"};
cursor= new MatrixCursor(columns);

//----------------- In asyctask doinbackground

cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});

When i try to get number of columns in the cursor the app simply crashes

protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();
}

UPDATE:

 private class getdealerinfo extends AsyncTask<Void,Void,MatrixCursor> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }


            @Override
            protected  MatrixCursor doInBackground(Void... args) {

                /* Building Parameters */

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pname",selectedproduct));
                params.add(new BasicNameValuePair("location",selectedlocation));

                /* getting JSON string from URL */
                JSONObject json = jParser.makeHttpRequest(dealers_url, "GET", params);

                try {
                    /* Checking for SUCCESS TAG */
                    int success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        JSONArray JAStuff = json.getJSONArray(TAG_STUFF);

                        /** CHECK THE NUMBER OF RECORDS **/
                        int intStuff = JAStuff.length();
                        //creating array 
                        dealerdetailsarray =new String[intStuff][12];
                        if (intStuff != 0) {
                             String[] str1 = new String[JAStuff.length()]; 
                             for(int i=0;i<JAStuff.length();i++)
                             {
                             json=JAStuff.getJSONObject(i);
                            // startManagingCursor(cursor);
                  //         dealerdetailsarray[0][0]=json.getString("dealername");
                             cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});

                             } 
                             }
                    }
                } catch (Exception e) {
              Log.e("test",e.toString());
                }
                return cursor; 
            }


            @Override
            protected void onPostExecute(MatrixCursor cursor) {
            //    super.onPostExecute(aVoid);
             try
             {
              Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();
             }
             catch (Exception e) {
                  Log.e("myerror",e.getMessage());
                    } 
       }
        }

    }
Unpremeditated answered 9/3, 2015 at 10:47 Comment(7)
are you getting NullPointerException?Bookbinding
@Bookbinding I tried adding catch (Exception e) { Log.e("test",e.toString()); } But nothing is catched.Unpremeditated
what is the stack trace?Bookbinding
@Bookbinding Logcat gives nothing.Please see the updateUnpremeditated
and where do you create your cursor?Bookbinding
@Bookbinding In the MainActivity protected void onCreateUnpremeditated
follow the answer by @ZygoteInit, that girl knows what she is sayingBookbinding
A
1

You need to return the cursor object from doInBackground() to onPostExecute(). Define your AsyncTask class as

class XYZTask extends AsyncTask<Void, Void, MatrixCursor>{

    protected MatrixCursor doInBackground(Void... void) {

         .....
         cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});
         return cursor;
    }

    protected void onPostExecute(MatrixCursor cursor) {
        Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();
    }


}

and return the cursor object from doInBackground() as a parameter to onPostExecute().

EDIT:

Define your AsyncTask like this:

private class getdealerinfo extends AsyncTask<Void,Void,JSONObject> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }


            @Override
            protected  JSONObject doInBackground(Void... args) {

                /* Building Parameters */

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pname",selectedproduct));
                params.add(new BasicNameValuePair("location",selectedlocation));

                /* getting JSON string from URL */
                JSONObject json = jParser.makeHttpRequest(dealers_url, "GET", params);


                return json; 
            }


            @Override
            protected void onPostExecute(JSONObject json) {
            //    super.onPostExecute(aVoid);
            MatrixCursor cursor = new MatrixCursor();

             try {
                    /* Checking for SUCCESS TAG */
                    int success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        JSONArray JAStuff = json.getJSONArray(TAG_STUFF);

                        /** CHECK THE NUMBER OF RECORDS **/
                        int intStuff = JAStuff.length();
                        //creating array 
                        dealerdetailsarray =new String[intStuff][12];
                        if (intStuff != 0) {
                             String[] str1 = new String[JAStuff.length()]; 
                             for(int i=0;i<JAStuff.length();i++)
                             {
                             json=JAStuff.getJSONObject(i);
                            // startManagingCursor(cursor);
                  //         dealerdetailsarray[0][0]=json.getString("dealername");
                             cursor.addRow(new String[] {json.getString("dealername"),json.getString("product"), json.getString("type"),json.getString("description"),json.getString("location"),json.getString("sublocation"),json.getString("address"),json.getString("phone1"),json.getString("phone2"),json.getString("auth"),json.getString("brands"),json.getString("lat"),json.getString("lon")});

                             } 
                             }
                    }
                } catch (Exception e) {
              Log.e("test",e.toString());
                }
             Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();

       }
        }

    }

Try this first.

Ascham answered 9/3, 2015 at 10:51 Comment(9)
hmm... How? Please add more detailsUnpremeditated
Please see the update.I tried catching the error Logcat shows nothingUnpremeditated
without the logcat error it will be extremely difficult to help you ... so far i am basing my answers on guesswork and trying to pin down the common sources or error.Ascham
i tried using the tag 'test' .But nothing is printed.Is there any way i can see more detailsUnpremeditated
in your IDE settings make sure that ALL logcat messages, including the lowest-priority ones, are displayedAscham
Here is the full log car error tag messages paste.ofcode.org/37CqtZFTTSN9pHjKPhsnMtMUnpremeditated
the error that you have posted doesn't seem to have anything to do with the AsyncTask or the cursor ... it seems to be related to something elseAscham
@techno: where have you reached with this ?Ascham
Thanks for your effort.But the problem was undefined Index Column for Cursor.Unpremeditated

© 2022 - 2024 — McMap. All rights reserved.