我创建了一个列表 activty1 和一个 activity2。在列表活动中,列表由数据库填充,在列表项上选择的活动2 被调用。在活动 2 中,我正在更新 mysql 数据库中的值。当我回调列表 activity1 时,正在添加另一个列表项,而不是被按下的列表项得到更新。数据库表中的值更新正常。请建议。
这是我的列表活动
@SuppressLint("UseValueOf")
public class DocPresc extends ListActivity {
//public static Context ctx;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String,String>>();
String pid;
JSONArray products = null;
EditText ailm,date,comment;
Button delete;
ListAdapter adapter;
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_patient_presc = "http://192.168.44.208/get_prescription.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
// private static final String TAG_PATIENT_ID = "patient_id";
private static final String TAG_AIL = "ailment";
private static final String TAG_MED = "medicine_name";
private static final String TAG_D1 = "qty1";
private static final String TAG_D2 = "qty2";
private static final String TAG_D3 = "qty3";
private static final String TAG_DATE = "prescription_date";
private static final String TAG_COM = "comment";
private static final String TAG_DID = "dosage_id";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.docpresc);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent i = getIntent();
Bundle extras = i.getExtras();
pid = extras.getString("TAG_PATIENT_ID");
System.out.println("Docpresc"+pid);
ailm = (EditText)findViewById(R.id.ailment1);
date = (EditText)findViewById(R.id.date1);
comment = (EditText)findViewById(R.id.comment1);
// if (savedInstanceState == null) {
new LoadPrescriptions().execute();
// }
}
class LoadPrescriptions extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
runOnUiThread(new Runnable() {
public void run() {
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
params.add(new BasicNameValuePair("patient_id",pid));//search1.getText().toString()));
System.out.println("database"+pid);
JSONObject json = jParser.makeHttpRequest(url_patient_presc, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Patients: ", json.toString());
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
//-----------------------------------
/*JSONObject product = products.getJSONObject(0);
ailm = (EditText)findViewById(R.id.ailment1);
ailm.setText(product.getString(TAG_AIL));*/
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String medicine = c.getString(TAG_MED).toUpperCase();
String qty1 = c.getString(TAG_D1).toUpperCase();
String qty2 = c.getString(TAG_D2).toUpperCase();
String qty3 = c.getString(TAG_D3).toUpperCase();
String dsg_id = c.getString(TAG_DID).toUpperCase();
//String ail = c.getString(TAG_AIL).toUpperCase();
ailm.setText(c.getString(TAG_AIL));
date.setText(c.getString(TAG_DATE));
comment.setText(c.getString(TAG_COM));
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_MED, medicine);
map.put(TAG_D1,qty1);
map.put(TAG_D2,qty2);
map.put(TAG_D3,qty3);
map.put(TAG_DID,dsg_id);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
//ListAdapter
adapter = new SimpleAdapter(
DocPresc.this, productsList,
R.layout.list_item2, new String[] {
TAG_MED,TAG_D1,TAG_D2,TAG_D3,TAG_DID},
new int[] {R.id.med,R.id.d1,R.id.d2,R.id.d3,R.id.did });
// updating listview
//setListAdapter(adapter);
setListAdapter(adapter);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
//menuSearch = menu.findItem(R.id.delete);
//menuSearch.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.add:
Bundle bundle = new Bundle();
bundle.putString("TAG_PATIENT_ID",pid );
System.out.println("bundle"+pid);
Intent i = new Intent(DocPresc.this,AddPresc.class);
i.putExtras(bundle);
startActivityForResult(i,100);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onListItemClick(ListView lv, View v, int position, long id) {
// TODO Auto-generated method stub
//onAttach(getActivity());
//lv.setAdapter(adapter);
String did1 = ((TextView) v.findViewById(R.id.did)).getText().toString();
String med1 = ((TextView) v.findViewById(R.id.med)).getText().toString();
String dg1 = ((TextView) v.findViewById(R.id.d1)).getText().toString();
String dg2 = ((TextView) v.findViewById(R.id.d2)).getText().toString();
String dg3 = ((TextView) v.findViewById(R.id.d3)).getText().toString();
// System.out.println("all patient"+id1);
Bundle bundle = new Bundle();
bundle.putString("TAG_DOSAGE_ID",did1 );
bundle.putString("TAG_DOSAGE_ID1",med1 );
bundle.putString("TAG_DOSAGE_ID2",dg1 );
bundle.putString("TAG_DOSAGE_ID3",dg2 );
bundle.putString("TAG_DOSAGE_ID4",dg3 );
// System.out.println("bundle"+id1);
Intent i = new Intent(DocPresc.this,EditPresc.class);
i.putExtras(bundle);
startActivityForResult(i,100);
//passData(date);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
/*Intent intent = getIntent();
finish();
startActivity(intent);*/
// ((SimpleAdapter)getListAdapter())
// ((SimpleAdapter) adapter).notifyDataSetChanged();
//getListView().setAdapter(null);
//getListView().refreshDrawableState();
new LoadPrescriptions().execute();
//adapter.notifyDataSetChanged()
}
}
}
这是在按下的列表项上调用的活动
public class EditPresc extends Activity implements View.OnClickListener{
//public static Context ctx;
EditText medicine;
EditText dosage1;
EditText dosage2;
EditText dosage3;
Button edit;
ImageButton up1 , up2,up3;
ImageButton down1,down2,down3;
String did,medi,q1,q2,q3;
int count = 1;
//JSONArray products = null;
//int pid = "100";
//private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
// private static final String url_getdosage = "http://192.168.44.208/get_dosage.php";
private static final String url_updatedosage = "http://192.168.44.208/update_dosage.php";
private static final String url_getmedname = "http://192.168.44.208/getmedname.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
// private static final String TAG_PATIENT_ID = "patient_id";
private static final String TAG_MED_ID ="medicine_id";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editpresc);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent i = getIntent();
Bundle extras = i.getExtras();
did = extras.getString("TAG_DOSAGE_ID");
medi = extras.getString("TAG_DOSAGE_ID1");
q1 = extras.getString("TAG_DOSAGE_ID2");
q2 = extras.getString("TAG_DOSAGE_ID3");
q3 = extras.getString("TAG_DOSAGE_ID4");
medicine = (EditText)findViewById(R.id.atxt1);
dosage1 = (EditText)findViewById(R.id.text1);
dosage2 = (EditText)findViewById(R.id.text2);
dosage3 = (EditText)findViewById(R.id.text3);
medicine.setText(medi);
dosage1.setText(q1);
dosage2.setText(q2);
dosage3.setText(q3);
System.out.println("Editpresc"+did);
//send = (Button)findViewById(R.id.b1);
up1=(ImageButton)findViewById(R.id.up1);
down1=(ImageButton)findViewById(R.id.down1);
dosage1=(EditText)findViewById(R.id.text1);
up2=(ImageButton)findViewById(R.id.up2);
down2=(ImageButton)findViewById(R.id.down2);
dosage2=(EditText)findViewById(R.id.text2);
up3=(ImageButton)findViewById(R.id.up3);
down3=(ImageButton)findViewById(R.id.down3);
dosage3=(EditText)findViewById(R.id.text3);
//dosage1.setText("1");
up1.setOnClickListener(this);
up2.setOnClickListener(this);
up3.setOnClickListener(this);
down1.setOnClickListener(this);
down2.setOnClickListener(this);
down3.setOnClickListener(this);
//ailment = (EditText)findViewById(R.id.atxt);
//comment = (EditText)findViewById(R.id.ctxt);
//presc=(EditText)findViewById(R.id.presc_id);
edit = (Button)findViewById(R.id.save);
// new GetDosage().execute();
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new GetMedname().execute();
new EditDosageDetails().execute();
}
});
}
private class GetMedname extends AsyncTask<String, Void, String> {
// JSONObject product;
protected String doInBackground(String... args) {
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
String med1 = medicine.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("medicine_name",med1));//search1.getText().toString()));
JSONObject json = jsonParser.makeHttpRequest(
url_getmedname, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
if(product!= null){
// display product data in EditText
medicine.setText(product.getString(TAG_MED_ID));
}
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
//return product;
return null;
}
}
private class EditDosageDetails extends AsyncTask { // JSONObject 产品;
protected String doInBackground(String... args) {
//JSONObject product = null;
//id.setText(100);
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
String med = medicine.getText().toString();
System.out.println("editdosage"+med);
String do1 = dosage1.getText().toString();
String do2 = dosage2.getText().toString();
String do3 = dosage3.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("medicine_id", med));
params.add(new BasicNameValuePair("dosage_id", did));
params.add(new BasicNameValuePair("qty1", do1));
params.add(new BasicNameValuePair("qty2", do2));
params.add(new BasicNameValuePair("qty3", do3));
JSONObject json = jsonParser.makeHttpRequest(url_updatedosage,
"POST", params);
// json success tag
// Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = getIntent();
setResult(100,i);
finish();
// super.onBackPressed();
// closing this screen
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
// }
}
});
//return product;
return null;
// });
// return product;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
//menuSearch = menu.findItem(R.id.delete);
//menuSearch.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.up1:
int a=Integer.parseInt(dosage1.getText().toString());
int b = a+1;
dosage1.setText(new Integer(b).toString());
break;
case R.id.down1:
a=Integer.parseInt(dosage1.getText().toString());
b = a-1;
dosage1.setText(new Integer(b).toString());
break;
case R.id.up2:
a=Integer.parseInt(dosage2.getText().toString());
b = a+1;
dosage2.setText(new Integer(b).toString());
break;
case R.id.down2:
a=Integer.parseInt(dosage2.getText().toString());
b = a-1;
dosage2.setText(new Integer(b).toString());
case R.id.up3:
a=Integer.parseInt(dosage3.getText().toString());
b = a+1;
dosage3.setText(new Integer(b).toString());
break;
case R.id.down3:
a=Integer.parseInt(dosage3.getText().toString());
b = a-1;
dosage3.setText(new Integer(b).toString());
break;
//case R.id.save:
// System.out.println("save pressed");
}
}
}