Vous êtes sur la page 1sur 14

Bundle gotBundle = getIntent().

getExtras();
searchcode=gotBundle.getString("searchcode");
JParse jsonParse = new JParse();
jsonParse.execute();
}

private class JParse extends AsyncTask<String, String, JSONObject> {


@Override
protected void onPreExecute() {
super.onPreExecute();
Name=(TextView)findViewById(R.id.valueGenericName);
Constituent=(TextView)findViewById(R.id.valueConstituent);
Cost=(TextView)findViewById(R.id.valuePrice);
Tax=(TextView)findViewById(R.id.valueTax);
Manufacturer=(TextView)findViewById(R.id.valueManufacturer);
Package=(TextView)findViewById(R.id.valuePackage);
Code=(TextView)findViewById(R.id.valueCode);
}
@Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
JParser jParser = new JParser();
JSONObject json = jParser.getJSONFromURL(searchcode);
}

return json;

@Override
protected void onPostExecute(JSONObject json){
// TODO Auto-generated method stub
super.onPostExecute(json);
try {
Drug = json.getJSONArray(TAG_OBJ);
for (int i = 0; i < Drug.length(); i++) {
JSONObject c = Drug.getJSONObject(i);
String drugName = c.getString(TAG_NAME);
if(drugName.length()!=0)
Name.setText(drugName);
else
Name.setText("Not Available");
String drugcode=c.getString(TAG_CODE);
if(drugcode.length()!=0)
Code.setText(drugcode);
else
Code.setText("Not Available");
String drugMan =
c.getString(TAG_MANUFACTURER);
if(drugMan.length()!=0)
Manufacturer.setText(drugMan);
else

Manufacturer.setText("Not Available");
String drugPacking = c.getString(TAG_PACKING);
if(drugPacking.length()!=0)
Package.setText(drugPacking);
else
Package.setText("Not Available");
String drugCost = c.getString(TAG_COST);
if(drugCost.length()!=0)
Cost.setText(drugCost);
else
Cost.setText("Not Available");
String drugTax = c.getString(TAG_TAX);
if(drugTax.length()!=0)
Tax.setText(drugTax);
else
Tax.setText("Not Available");
String drugContent = c.getString(TAG_CONTENT);
if(drugContent.length()!=0)
Constituent.setText(drugContent);
else
Constituent.setText("Not Available");
}
}
catch (JSONException e) {
e.printStackTrace();
}
}

----------------------------------------------------------------------------------

InputStream inputstream = null;

JSONObject jObj = null;


String json = "";
public JSONObject getJSONFromURL(String searchCode){
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("searchcode",searchCode));
try{

DefaultHttpClient httpClient = new DefaultHttpClient();


String Url="http://10.0.2.2/getDrugResult.php";
HttpPost httpPost = new HttpPost(Url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
inputstream = entity.getContent();

}
catch(UnsupportedEncodingException e){
e.printStackTrace();
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
try{
BufferedReader br = new BufferedReader(new
InputStreamReader(inputstream,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine())!=null){
sb.append(line);
}
inputstream.close();
json = sb.toString();
}
catch(Exception e){
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try{
jObj = new JSONObject(json);
}
catch(JSONException e){
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}

List Result of old


public class ListResult extends Activity {
ListView list;
TextView drugName;
TextView drugStrength;
TextView drugMan;
TextView prepType;
TextView drugCost;
TextView drugCompare;
ArrayList<HashMap<String, String>> druglist = new ArrayList<HashMap<String,
String>>();
// private static String url =
// "http://10.0.2.2:8080/getDrugfromBrandName.php";
private static final String TAG_NAME = "DrugName";
private static final String TAG_STRENGTH = "DrugStrength";
private static final String TAG_MANUFACTURER = "DrugManufacturer";
private static final String TAG_PREPTYPE = "DrugPrepType";
private static final String TAG_COST = "DrugCost";
private static final String TAG_OBJ = "drug";
JSONArray Drug = null;
String gotSearchType;
String gotSearchText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result_list);
Bundle gotBundle = getIntent().getExtras();
gotSearchType = gotBundle.getString("searchtype");
gotSearchText = gotBundle.getString("searchtext");
druglist = new ArrayList<HashMap<String, String>>();
JSONParse jsonParse = new JSONParse();
jsonParse.execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
drugName = (TextView) findViewById(R.id.tvName);
drugStrength = (TextView) findViewById(R.id.tvStrength);
drugMan = (TextView) findViewById(R.id.tvMAnufacturer);
prepType = (TextView) findViewById(R.id.tvType);
drugCost = (TextView) findViewById(R.id.tvPrice);
//drugCompare = (TextView) findViewById(R.id.tvCompare);
}

@Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromURL(gotSearchType,
gotSearchText);
return json;
}

String>();

@Override
protected void onPostExecute(JSONObject json) {
// TODO Auto-generated method stub
super.onPostExecute(json);
try {
Drug = json.getJSONArray(TAG_OBJ);
for (int i = 0; i < Drug.length(); i++) {
JSONObject c = Drug.getJSONObject(i);
String drugName = c.getString(TAG_NAME);
String drugStrength = c.getString(TAG_STRENGTH);
String drugMan = c.getString(TAG_MANUFACTURER);
String prepType = c.getString(TAG_PREPTYPE);
String drugCost = c.getString(TAG_COST);
HashMap<String, String> map = new HashMap<String,
map.put(TAG_NAME, drugName);
map.put(TAG_STRENGTH, drugStrength);
map.put(TAG_MANUFACTURER, drugMan);
map.put(TAG_PREPTYPE, prepType);
map.put(TAG_COST, drugCost);
druglist.add(map);
list = (ListView) findViewById(R.id.resultList);
ListAdapter adapter = new

SimpleAdapter(ListResult.this,
TAG_MANUFACTURER,

druglist, R.layout.list_item, new String[] {


TAG_NAME, TAG_STRENGTH,
TAG_PREPTYPE,

TAG_COST }, new int[] {

R.id.tvName,

R.id.tvStrength,

R.id.tvMAnufacturer,

R.id.tvType,

R.id.tvPrice });
list.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}

}
}

DrugResult.xml add
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:id="@+id/titleCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Code"
android:visibility="invisible"/>"
<TextView
android:id="@+id/valueCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
android:gravity="left"
android:text="4.00"
android:visibility="invisible"/>"
</LinearLayout>

ListResult old
package com.example.gdf;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ListResult extends Activity {

ListView list;

TextView drugCode;
TextView drugName;
TextView drugPacking;
TextView drugMan;
//TextView prepType;
TextView drugCost;
//TextView drugCompare;

ArrayList<HashMap<String, String>> druglist = new


ArrayList<HashMap<String, String>>();
//private static String url ="http://10.0.2.2/getDrugfromBrandName2.php";
private static final String TAG_NAME = "drugname";
//private static final String TAG_STRENGTH = "DrugStrength";
private static final String TAG_MANUFACTURER = "manufacturer";
private static final String TAG_PACKING = "packing";
private static final String TAG_COST = "mrp";
private static final String TAG_CODE = "drugcode";
private static final String TAG_OBJ = "drug";
JSONArray Drug = null;
String gotSearchType;
String gotSearchText;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

Bundle gotBundle = getIntent().getExtras();


gotSearchType = gotBundle.getString("searchtype");
gotSearchText = gotBundle.getString("searchtext");
setContentView(R.layout.result_list);
druglist = new ArrayList<HashMap<String, String>>();

JSONParse jsonParse = new JSONParse();


jsonParse.execute();

private class JSONParse extends AsyncTask<String, String, JSONObject> {

@Override
protected void onPreExecute() {
super.onPreExecute();

drugName = (TextView) findViewById(R.id.tvName);


drugPacking = (TextView) findViewById(R.id.tvPacking);
drugMan = (TextView) findViewById(R.id.tvManufacturer);
//prepType = (TextView) findViewById(R.id.tvType);
drugCost = (TextView) findViewById(R.id.tvPrice);
drugCode =(TextView)findViewById(R.id.tvCode);
//

drugCode.setVisibility(View.INVISIBLE);

//drugCompare = (TextView) findViewById(R.id.tvCompare);


}

@Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromURL(gotSearchType,
gotSearchText);
return json;
}

/*public void selectActivity(String code) {


// TODO Auto-generated method stub

Bundle bundle = new Bundle();


bundle.putString("searchcode", code);

Intent intent = new Intent(ListResult.this,DrugResult.class);


intent.putExtras(bundle);
startActivity(intent);

}*/

@Override
protected void onPostExecute(JSONObject json) {
// TODO Auto-generated method stub
super.onPostExecute(json);
try {
Drug = json.getJSONArray(TAG_OBJ);
for (int i = 0; i < Drug.length(); i++) {
JSONObject c = Drug.getJSONObject(i);
String drugName = c.getString(TAG_NAME);
String drugcode=c.getString(TAG_CODE);
//

String drugStrength = c.getString(TAG_STRENGTH);

String drugMan =
c.getString(TAG_MANUFACTURER);
String drugPacking = c.getString(TAG_PACKING);
String drugCost = "Rs. " + c.getString(TAG_COST);

HashMap<String, String> map = new


HashMap<String, String>();
map.put(TAG_NAME, drugName);
map.put(TAG_CODE, drugcode);
//map.put(TAG_STRENGTH, drugStrength);
map.put(TAG_MANUFACTURER, drugMan);
map.put(TAG_PACKING, drugPacking);
map.put(TAG_COST, drugCost);
druglist.add(map);
list = (ListView) findViewById(R.id.resultList);

ListAdapter adapter = new


SimpleAdapter(ListResult.this,
druglist, R.layout.list_item, new
String[] {
TAG_NAME,TAG_CODE,
TAG_MANUFACTURER,
TAG_PACKING,TAG_COST }, new int[] {
R.id.tvName,R.id.tvCode,
R.id.tvManufacturer,
R.id.tvPacking,
R.id.tvPrice });

list.setAdapter(adapter);
//

drugCode.setVisibility(View.INVISIBLE);

/*

list.setOnItemClickListener(new

OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?>
arg0, View arg1,
int arg2, long arg3) {

String
getCode=drugCode.getText().toString();
selectActivity(getCode);
// TODO Auto-generated method stub

});
*/

} catch (JSONException e) {
e.printStackTrace();

}
}

}
<Button
android:id="@+id/Rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:background="@drawable/rounded"
android:padding="15dp"
android:radius="10dp"
android:text="Rate Me"
android:textSize="15dp"/>
<Button
android:id="@+id/Information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:background="@drawable/rounded"

android:padding="15dp"
android:textSize="15dp"
android:text="Find Me" />

Vous aimerez peut-être aussi