Android:从数据库得到响应后,返回上一个屏幕
有人能帮我吗。。??我的问题是,在从json获得响应后[它来自webservice,因为这里有Select query来获取记录],而不是在listview中显示返回上一个屏幕这里是我的示例代码Android:从数据库得到响应后,返回上一个屏幕,android,json,listview,Android,Json,Listview,有人能帮我吗。。??我的问题是,在从json获得响应后[它来自webservice,因为这里有Select query来获取记录],而不是在listview中显示返回上一个屏幕这里是我的示例代码 public class MainActivity5 extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
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();
}
}
}
公共类MainActivity5扩展了ListActivity{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_列表_视图);
Intent i=getIntent();
phNumber=i.getStringExtra(“phn_编号”);
新建SearchCustomer().execute();
ListAdapter=new SimpleAdapter(this,customerList,R.layout.activity_main5,新字符串[]{TAG\u F\u NAME,TAG\u C\u PHONE},新int[]{R.id.TextViewName,R.id.TextViewPhone});
setListAdapter(适配器);
}
类SearchCustomer扩展异步任务{
私有布尔成功标志;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity5.this);
pDialog.setMessage(“搜索客户…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
字符串inputphnotext=phNumber;
List params=new ArrayList();
参数add(新的BasicNameValuePair(“phno”,inputphnText));
JSONObject json=jsonParser.makeHttpRequest(url\u search\u customer,“POST”,params);
Log.d(“搜索响应”,json.toString());
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
successFlag=true;
result=json.getJSONArray(TAG_result);
for(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签名称,fname);
地图放置(标记电话、电话号码);
//将哈希列表添加到ArrayList
customerList.add(地图);
}
}
否则{
successFlag=false;
}
}
捕获(JSONException e){
Log.e(“Log_标记”,“错误解析数据”+e.toString());
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
完成();
}
}
}
这是我的活动文件代码。。在doinbackground customerList中,我得到了我想要的确切数据,但问题是它没有停留在custom_list_view屏幕中,而是返回到上一个屏幕,其中有文本框和搜索按钮。。所以无法显示记录
各位,有谁能解决这个愚蠢的问题吗?只需在代码中删除onPostExecute中的finish()
protected void onPostExecute(String file_url) {
pDialog.dismiss();
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);
}
只需在代码中从onPostExecute中删除finish()
protected void onPostExecute(String file_url) {
pDialog.dismiss();
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);
}
不要在
onPostExecute()
方法中完成活动。不要在onPostExecute()
方法中完成活动。
完成()
方法关闭您的活动,以便将其删除。
完成()
方法关闭您的活动,以便将其删除
onCreate()
中调用create adapter和set adapter,而是可以在onPostExecute()中执行
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);
}
for(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签名称,fname);
地图放置(标记电话、电话号码);
//将哈希列表添加到ArrayList
customerList.add(地图);
}
无需进行这种开销,您可以通过扩展ArrayAdapter
来创建自定义适配器,因此无需准备HashMap
,而是可以直接添加JSONObject
onCreate()
中调用create adapter和set adapter,而是可以在onPostExecute()中执行
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);
}
for(int i=0;ivalue
地图放置(标签标识,标识);