Can any one help me..?? My problem is that, after getting response from json [its coming from webservice, in that there is Select query to fetch records], instead of display in listview its go back to previous screen Here is my sample code for that..
public class MainActivity5 extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
Intent i = getIntent();
phNumber = i.getStringExtra("phn_no");
new SearchCustomer().execute();
ListAdapter adapter = new SimpleAdapter(this, customerList, R.layout.activity_main5, new String[] { TAG_F_NAME, TAG_C_PHONE }, new int[] { R.id.TextViewName, R.id.TextViewPhone });
setListAdapter(adapter);
}
class SearchCustomer extends AsyncTask<String, String, String> {
private boolean successFlag;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity5.this);
pDialog.setMessage("Searching Customer..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String inputPhnNoText = phNumber;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("phno", inputPhnNoText));
JSONObject json = jsonParser.makeHttpRequest(url_search_customer, "POST", params);
Log.d("Search Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
successFlag = true;
result = json.getJSONArray(TAG_RESULT);
for (int i = 0; i < result.length(); i++) {
JSONObject c = result.getJSONObject(i);
String id = c.getString(TAG_ID);
String fname = c.getString(TAG_F_NAME);
String phoneNumber = c.getString(TAG_C_PHONE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_F_NAME, fname);
map.put(TAG_C_PHONE, phoneNumber);
// adding HashList to ArrayList
customerList.add(map);
}
}
else {
successFlag = false;
}
}
catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
finish();
}
}
}
This is my activity file code.. in doinbackground customerList I got the exact data what I want, but problem is that its not stay in the custom_list_view screen its go back to previous screen where a textbox and search button are there.. so not able to show record…
Everybody please, can any one give solution to this silly thing?
Agree with CapDroid regarding finish(), don’t do it.
Don’t call create adapter and set adapter inside
onCreate(), instead you can do it insideonPostExecute().You are doing 2 overheads, Don’t do those.
For example:
No need to do this kind of overhead, instead you can create a custom adapter by extending
ArrayAdapter<JSONObject>so you don’t need to prepareHashMap, instead you can addJSONObjectdirectly.