我使用矩阵光标向光标添加行。
在Create
上的MainActivity中定义列//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);
// -----------------在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")});
当我尝试获取光标中的列数时,应用程序只会崩溃
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(), cursor.getCount(),Toast.LENGTH_LONG).show();
}
更新:
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());
}
}
}
}
答案 0 :(得分:1)
您需要将cursor
对象从doInBackground()
返回到onPostExecute()
。将您的AsyncTask
课程定义为
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();
}
}
并将cursor
对象从doInBackground()
作为参数返回到onPostExecute()
。
修改强>
像这样定义AsyncTask
:
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();
}
}
}
先试试这个。