Created
July 22, 2016 21:08
-
-
Save EleLawliet/f4fffcc6ceb14ef3969917fb042e48a6 to your computer and use it in GitHub Desktop.
Spinner Con Data // ID - VALOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConsultaEstadoTramiteActivity extends BaseActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener { | |
private Spinner spinnerRegistromercantil; | |
public List<DatoGenerico> lista1; | |
private CustomAdapter customAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
//No le pares bola a esto, tu utiliza lo que normalmente se utiliza aqui que es SetContentView(R.....); | |
Bundle parametros = new Bundle(); | |
parametros.putInt("layout", R.layout.activity_consulta_estado_tramite); | |
parametros.putInt("verbtnatras", 1); | |
parametros.putInt("vertoolbar", 1); | |
super.onCreate(parametros); | |
lista1 = new ArrayList<DatoGenerico>(); | |
spinnerRegistromercantil = (Spinner) findViewById(R.id.spinner_registros_mercantiles); | |
traerInfoSpinner(); | |
customAdapter = new CustomAdapter(getApplicationContext(), lista1); | |
spinnerRegistromercantil.setAdapter(customAdapter); | |
spinnerRegistromercantil.setOnItemSelectedListener(this); | |
spinnerRegistromercantil.setSelected(true); | |
//spinnerRegistromercantil.setPrompt("Titulo del spinner"); | |
spinnerRegistromercantil.setOnItemSelectedListener(this); | |
} | |
public void traerInfoSpinner() { | |
try { | |
String newURL = "http:\\"; | |
try { | |
RequestApp request = new RequestApp(Request.Method.POST, newURL, new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject jsonobj) { | |
boolean exito = jsonobj.optBoolean("resultado"); | |
if (exito) { | |
JSONArray lista = jsonobj.optJSONArray("lista"); | |
Log.i("url", "tamaño lista " + lista); | |
for (int i = 0; i < lista.length(); i++) { | |
try { | |
DatoGenerico items = new DatoGenerico(); | |
items.setNombreEmpresa(lista.getJSONObject(i).getString("nombreEmpresa")); | |
items.setEmpresaId(lista.getJSONObject(i).getString("empresaId")); | |
customAdapter.agregarData(items); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} .... el codigo siguiente es tuyo. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomAdapter extends BaseAdapter { | |
Context context; | |
List<DatoGenerico> nombreCantones; | |
LayoutInflater inflter; | |
TextView nombre_empresa; | |
public CustomAdapter(Context applicationContext, List<DatoGenerico> nombreCantones) { | |
this.context = applicationContext; | |
this.nombreCantones = nombreCantones; | |
inflter = (LayoutInflater.from(applicationContext)); | |
} | |
@Override | |
public int getCount() { | |
return nombreCantones.size(); | |
} | |
@Override | |
public Object getItem(int i) { | |
return null; | |
} | |
@Override | |
public long getItemId(int i) { | |
return 0; | |
} | |
@Override | |
public View getView(int i, View convertView, ViewGroup viewGroup) { | |
ViewHolder holder = null; | |
if (convertView == null) { | |
convertView = inflter.inflate(R.layout.spinner_escoja_sorteo, null); | |
holder = new ViewHolder(); | |
holder.nombre_empresa = (TextView) convertView.findViewById(R.id.tv_nombre); | |
holder.nombre_empresa.setSingleLine(false); | |
holder.nombre_empresa.setEllipsize(TextUtils.TruncateAt.END); | |
int n = 2; // the exact number of lines you want to display | |
holder.nombre_empresa.setLines(n); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
DatoGenerico objectItem = nombreCantones.get(i); | |
// assign values if the object is not null | |
if(objectItem != null) { | |
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values | |
holder.nombre_empresa.setText(objectItem.getNombreEmpresa()); | |
} | |
return convertView; | |
} | |
static class ViewHolder { | |
TextView nombre_empresa ; | |
} | |
public void agregarData(DatoGenerico dao) { | |
nombreCantones.add(dao); | |
notifyDataSetChanged(); | |
} | |
public DatoGenerico getCantones(int pos) { | |
return nombreCantones.get(pos); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Junior on 13/07/2016. | |
*/ | |
public class DatoGenerico { | |
/** | |
* empresaId : representa el codigo o valor unico | |
* nombreEmpresa : el estado civil (soltero casado divorciado etc) | |
*/ | |
private String empresaId; | |
private String nombreEmpresa; | |
public String getEmpresaId() { | |
return empresaId; | |
} | |
public void setEmpresaId(String empresaId) { | |
this.empresaId = empresaId; | |
} | |
public String getNombreEmpresa() { | |
return nombreEmpresa; | |
} | |
public void setNombreEmpresa(String nombreEmpresa) { | |
this.nombreEmpresa = nombreEmpresa; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment