Contenido sobre Android
Buscar
Social
Ofertas laborales ES

Foro sobre Android > intents,arraylist


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class ListadoProductos extends Activity {

protected LinearLayout layout_02;
protected TextView text;
protected TextView cont;
protected LinearLayout layout_01;
protected Button regresa;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
String [] arreglo = bundle.getStringArray("productos");
this.cont=new TextView(this);
initLayout();
this.cont.setText(String.valueOf(arreglo.length) + "productos ingresados");
verProductos(arreglo);
Init_listener();
}

private void initLayout() {
// TODO Auto-generated method stub
this.layout_01 = new LinearLayout(this);
this.layout_01.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
this.layout_01.setLayoutParams(lp);
this.layout_01.addView(cont);
this.layout_02 = new LinearLayout(this);
layout_02.setOrientation(LinearLayout.VERTICAL);
layout_02.setLayoutParams(lp);
layout_01.addView(layout_02);
setContentView(layout_01);
}
private void verProductos(String productos []) {
// TODO Auto-generated method stub
for(int w=0;w<productos.length;w++)
{
text = new TextView(this);
text.setText(productos[w]);
if(layout_02.getChildCount() %2 == 0)
{
text.setBackgroundColor(Color.DKGRAY);
layout_02.addView(text);
}
else
{
text.setBackgroundColor(Color.WHITE);
layout_02.addView(text);
}
}
this.regresa = new Button(this);
regresa.setText("Volver");
layout_02.addView(regresa);


}
private void Init_listener()
{
regresa.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent (v.getContext(),ListadoProductos.class);
Intent intent = new Intent();
intent.setClass(ListadoProductos.this, PrincipalActivity.class);
startActivity(intent);
finish();


/*Intent intent = new Intent();
intent.setClass (ActividadUno.this, ActividadDos.class);
startActivity(intent);
finish();*/


}

});
}

}


//ventana 2
import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PrincipalActivity extends Activity {
/** Called when the activity is first created. */
protected LinearLayout layout;
protected int contador=0;
protected String[] nombresProductos;
protected TextView caja_texto;
protected TextView ctext;
protected EditText texto_ingreso;
protected Button boton_agregar,boton_visualizar;
protected ArrayList<Productos> arrProductos;
Intent xx;
String regresa="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();

this.init_Layout();
arrProductos = new ArrayList<Productos>();
nombresProductos = getResources().getStringArray(R.array.productoscargados);

for (int i=0;i<nombresProductos.length;i++)
{
addProducts(nombresProductos[i]);
}

Init_Listener();

}

private void init_Layout() {
// TODO Auto-generated method stub

this.layout = new LinearLayout(this);
this.layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams lay = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
this.layout.setLayoutParams(lay);
this.ctext = new TextView(this);
this.caja_texto=new TextView(this);
this.layout.addView(ctext);
caja_texto.setText(regresa);
this.layout.addView(caja_texto);
this.caja_texto = new TextView(this);
this.texto_ingreso = new EditText(this);
this.boton_agregar = new Button(this);
this.boton_visualizar = new Button(this);

//this.caja_texto.setGravity(Gravity.CENTER);

this.boton_agregar.setText("Ingrese producto");
this.boton_visualizar.setText("Visualizar Productos");
//
this.layout.addView(texto_ingreso);
this.layout.addView(boton_agregar);
this.layout.addView(boton_visualizar);

setContentView(layout);

}
private void Init_Listener() {

// TODO Auto-generated method stub
boton_agregar.setOnClickListener(new OnClickListener (){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(texto_ingreso.getText().toString().trim().compareToIgnoreCase("") > 0)
{
addProducts(texto_ingreso.getText().toString());
texto_ingreso.setText("");
}
}

});
this.texto_ingreso.setOnKeyListener(new View.OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

if(keyCode==KeyEvent.KEYCODE_ENTER)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(texto_ingreso.getWindowToken(), 0);
String nProd = texto_ingreso.getText().toString();
if(nProd.length()>0)
{
addProducts(nProd);
texto_ingreso.setText("");
}
return true;
}

return false;
}
});
boton_visualizar.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(),ListadoProductos.class);
String [] s = new String[arrProductos.size()];

for (int x = 0;x
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cl.ciisa.rquiroz"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />





<category android:name="android.intent.category.LAUNCHER" />




</manifest>


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PrincipalActivity!</string>
<string name="app_name">Solemne1</string>
<string-array name="productoscargados">
Papas fritas
Carne
Bebida
</string-array>
</resources>


noviembre 11, 2011 | Unregistered Commenterrquiroz