简体   繁体   中英

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

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

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM