这是我的活动代码:
public class resultadoscopia extends Activity implements datos.OnFragmentInteractionListener {
ImageView foto;
Button Sig;
Button Ant;
Button Adopta;
String hld;
String postal;
String animal;
String email;
String telf;
String edad;
String nomanimal;
String funciones;
String cchld;
String neces;
String ccanimal;
String ccpostal;
String nomcont;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
public String url_product_detials = "http://adoptia.esy.es/androptar.php";
// JSON Node names
public static final String TAG_HLD = "hld";
public static final String TAG_POSTAL = "postal";
public static final String TAG_ANIMAL = "animal";
public static final String TAG_SUCCESS = "success";
public static final String TAG_PRODUCT = "product";
public static final String TAG_EMAIL = "email";
public static final String TAG_TEL = "telf";
public static final String TAG_EDAD = "edad";
public static final String TAG_NANIMAL = "nomanimal";
public static final String TAG_NCONT = "nomcont";
public static final String TAG_NECES = "neces";
public static final String TAG_FUNCIONES = "funciones";
public static final String TAG_FOTO = "foto";
public Bitmap loadedImage;
int bCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resultados1);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Adopta = (Button)findViewById(R.id.add_fragment);
Adopta.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Paso 1: Obtener la instancia del administrador de fragmentos
FragmentManager fragmentManager = getFragmentManager();
//Paso 2: Crear una nueva transacción
FragmentTransaction transaction = fragmentManager.beginTransaction();
//Paso 3: Crear un nuevo fragmento y añadirlo
Bundle bundle = new Bundle();
String accanimal=ccanimal;
String aedad=edad;
String aemail=email;
String acchld=cchld;
String anomanimal=nomanimal;
String anomcont=nomcont;
String afunciones=funciones;
String aneces=neces;
String accpostal=ccpostal;
String atelf=telf;
bundle.putString("ccanimal",accanimal);
bundle.putString("edad",aedad);
bundle.putString("email",aemail);
bundle.putString("cchld",acchld);
bundle.putString("nomanimal",anomanimal);
bundle.putString("nomcont",anomcont);
bundle.putString("funciones",afunciones);
bundle.putString("neces",aneces);
bundle.putString("ccpostal",accpostal);
bundle.putString("telf",atelf);
datos fragInfo = new datos();
fragInfo.setArguments(bundle);
transaction.add(R.id.contenedor, fragInfo);
transaction.replace(R.id.contenedor, fragInfo);
//Paso 4: Confirmar el cambio
transaction.commit();
}
});
Sig = (Button)findViewById(R.id.btn_sig);
Ant = (Button)findViewById(R.id.btn_ant);
Bundle extras = getIntent().getExtras();
hld = extras.getString(TAG_HLD);
postal = extras.getString(TAG_POSTAL);
animal = extras.getString(TAG_ANIMAL);
foto = (ImageView)findViewById(R.id.cfoto);
// Getting complete product details in background thread
new GetProductDetails().execute();
Sig.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
bCount++;
new GetProductDetails().execute();
}
});
Ant.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
bCount--;
new GetProductDetails().execute();
}
});
}
public void irselect(View v){
Intent intent = new Intent(resultadoscopia.this, select.class);
startActivity(intent);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
/**
* Background Async Task to Get complete product details
* */
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(resultadoscopia.this);
pDialog.setMessage("Buscando animales...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("hld", hld));
params.add(new BasicNameValuePair("postal", postal));
params.add(new BasicNameValuePair("animal", animal));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
// check your log for json response
Log.d("animal", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
final JSONArray productObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(bCount);
// display product data in EditText
ccanimal=(product.getString(TAG_ANIMAL));
edad=(product.getString(TAG_EDAD));
email=(product.getString(TAG_EMAIL));
cchld=(product.getString(TAG_HLD));
nomanimal=(product.getString(TAG_NANIMAL));
nomcont=(product.getString(TAG_NCONT));
funciones=(product.getString(TAG_FUNCIONES));
neces=(product.getString(TAG_NECES));
ccpostal=(product.getString(TAG_POSTAL));
telf=(product.getString(TAG_TEL));
URL imageUrl;
try {
imageUrl = new URL("http","adoptia.esy.es",product.getString(TAG_FOTO));
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.connect();
loadedImage = BitmapFactory.decodeStream(conn.getInputStream());
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error cargando la imagen: " + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
foto.setImageBitmap(loadedImage);
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_resultados, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是片段代码:
public class datos extends Fragment {
EditText email;
EditText telf;
EditText edad;
EditText nomanimal;
EditText funciones;
EditText cchld;
EditText neces;
EditText ccanimal;
EditText ccpostal;
EditText nomcont;
private OnFragmentInteractionListener mListener;
public datos() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
String rAza = bundle.getString("ccanimal");
String hld = bundle.getString("cchld");
String postal = bundle.getString("ccpostal");
String emilio = bundle.getString("email");
String tel = bundle.getString("telf");
String edade = bundle.getString("edad");
String nomani = bundle.getString("nomanimal");
String nece = bundle.getString("neces");
String func = bundle.getString("funciones");
String nomcon = bundle.getString("nomcont");
View view = inflater.inflate(R.layout.fragment_datos, container, false);
//Nuevos parametros para el view del fragmento
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
//Nueva Regla: EL fragmento estara debajo del boton add_fragment
params.addRule(RelativeLayout.ABOVE, R.id.add_fragment);
//Margenes: top:41dp
params.setMargins(0,0,0,0);
//Setear los parametros al view
view.setLayoutParams(params);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ccanimal = (EditText)view.findViewById(R.id.canimal);
cchld = (EditText)view.findViewById(R.id.chdl);
ccpostal=(EditText)view.findViewById(R.id.cpostal);
email = (EditText)view.findViewById(R.id.cemail);
telf = (EditText)view.findViewById(R.id.ctelf);
edad = (EditText)view.findViewById(R.id.cedad);
nomanimal = (EditText)view.findViewById(R.id.cnomanimal);
nomcont = (EditText)view.findViewById(R.id.cnomcont);
neces =(EditText)view.findViewById(R.id.cneces);
funciones =(EditText)view.findViewById(R.id.cfunciones);
ccanimal.setText(rAza);
edad.setText(edade);
email.setText(emilio);
cchld.setText(hld);
nomanimal.setText(nomani);
nomcont.setText(nomcon);
funciones.setText(func);
neces.setText(nece);
ccpostal.setText(postal);
telf.setText(tel);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_datos, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}