En este ejemplo se muestra como mejorar la implementación del código que se muestra en la pregunta Como usar dos layouts diferentes en una lista (Listview) realizada en Stack Overflow en español.
-
-
Save david-minaya/5185b01a2a9ff2a825c1a52e323719c5 to your computer and use it in GitHub Desktop.
En este ejemplo se muestra como mejorar la implementación del código que se muestra en la pregunta "Como usar dos layouts diferentes en una lista (Listview)" realizada en Stack Overflow en español.
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 ActivityOne extends Fragment { | |
private String TAG = MainActivity.class.getSimpleName(); | |
private ListView lista_mostrando; | |
Adapter adaptador; | |
static ArrayList<Item> lista; | |
View view; | |
public static ActivityOne newInstance() { | |
ActivityOne fragment = new ActivityOne(); | |
return fragment; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
lista = new ArrayList<>(); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
view = inflater.inflate(R.layout.activity_one, container, false); | |
lista_mostrando = (ListView) view.findViewById(R.id.lista_llamadas); | |
new LlamadasYmensajes.execute(); | |
return view; | |
} | |
private class LlamadasYmensajes extends AsyncTask<Void, Void, Void> { | |
private ProgressDialog pDialog; | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
pDialog = new ProgressDialog(getActivity()); | |
pDialog.setMessage("Getting Data ..."); | |
pDialog.setIndeterminate(false); | |
pDialog.setCancelable(true); | |
pDialog.show(); | |
} | |
@Override | |
protected Void doInBackground(Void... arg0) { | |
llamadas(); | |
mensajes(); | |
return null; | |
} | |
public void llamadas() { | |
String datoNombreContacto = getActivity().getIntent().getStringExtra("contacto_detalle"); | |
String url = "http://10.0.2.2/xampp/BdApp/consultar_llamadas.php?nombre=" + datoNombreContacto; | |
HttpHandler sh = new HttpHandler(); | |
String jsonStr = sh.makeServiceCall(url); | |
Log.e(TAG, "Response from url: " + jsonStr); | |
if (jsonStr != null) { | |
try { | |
JSONArray jsonObj = new JSONArray(jsonStr); | |
for (int i = 0; i < jsonObj.length(); i++) { | |
JSONObject c = jsonObj.getJSONObject(i); | |
String idGroup = "llamadas" // Id de las llamadas | |
String nombre = c.getString("nombre"); | |
String fecha = c.getString("fecha"); | |
String duracion = c.getString("duracion"); | |
String tipo = c.getString("tipo"); | |
Item e=new Item(); | |
e.setIdGroup(idGroup) // Asignas el id | |
e.setNombre(nombre); | |
e.setFecha(fecha); | |
e.setDuracion(duracion); | |
e.setTipo(tipo); | |
lista.add(e); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} else { | |
Log.e("ServiceHandler", "Esta habiendo problemas para cargar el JSON"); | |
} | |
} | |
public void mensajes() { | |
String datoNumeroContacto = getActivity().getIntent().getStringExtra("numero_detalle"); | |
String url2 = "http://10.0.2.2/xampp/BdApp/consultar_mensajes.php?numero=" + datoNumeroContacto; | |
HttpHandler sh = new HttpHandler(); | |
String jsonStr = sh.makeServiceCall(url2); | |
Log.e(TAG, "Response from url: " + jsonStr); | |
if (jsonStr != null) { | |
try { | |
JSONArray jsonObj = new JSONArray(jsonStr); | |
for (int i = 0; i < jsonObj.length(); i++) { | |
JSONObject c = jsonObj.getJSONObject(i); | |
String idGroup = "mensajes"; // Id de los mensajes | |
String fecha = c.getString("fecha"); | |
String tipo = c.getString("tipo"); | |
Item e=new Item(); | |
e.setIdGroup(idGroup); // Asignas el id | |
e.setFecha(fecha); | |
e.setTipo(tipo); | |
lista.add(e); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
Log.e("ServiceHandler", "Esta habiendo problemas para cargar el JSON"); | |
} | |
} | |
@Override | |
protected void onPostExecute(Void result) { | |
super.onPostExecute(result); | |
if (pDialog.isShowing()){ | |
pDialog.dismiss(); | |
} | |
adaptador = new Adapter(getActivity(),lista); | |
lista_mostrando.setAdapter(adaptador); | |
} | |
} | |
} |
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 Adapter extends BaseAdapter { | |
private Context context; | |
private ArrayList<Item> arrayList; | |
private LayoutInflater layoutInflater; | |
Random random; | |
public Adapter(Context context, ArrayList<Item> arrayList){ | |
this.context = context; | |
this.arrayList = arrayList; | |
random = new Random(); | |
} | |
... | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if(convertView == null) { | |
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
convertView = layoutInflater.inflate(R.layout.item_llamadas, null); | |
} | |
ImageView iv_main = (ImageView) convertView.findViewById(R.id.iv_main); | |
TextView nombre = (TextView) convertView.findViewById(R.id.nombre); | |
ImageView tv_main = (ImageView) convertView.findViewById(R.id.tv_main); | |
TextView fecha = (TextView) convertView.findViewById(R.id.fecha); | |
TextView duracion = (TextView) convertView.findViewById(R.id.duracion); | |
TextView tipo = (TextView) convertView.findViewById(R.id.tipo); | |
// Evalua si los datos son de las llamadas | |
if (arrayList.get(position).getIdGroup().equals("llamadas")) { | |
nombre.setText(arrayList.get(position).getNombre()); | |
fecha.setText(arrayList.get(position).getFecha()); | |
duracion.setText(arrayList.get(position).getDuracion()); | |
tipo.setText(arrayList.get(position).getTipo()); | |
iv_main.setImageResource(R.drawable.circle); | |
int color = Color.argb(255, random.nextInt(250), random.nextInt(250), random.nextInt(250)); | |
iv_main.setColorFilter(color); | |
tv_main.setImageResource(R.drawable.ic_call_black_24dp); | |
} else if (arrayList.get(position).getIdGroup().equals("mensajes")) { // Evalua si los datos son de los mensajes | |
// Aqui le asignas un valor diferente a los items de los mensajes. No se, | |
// puede ser un color de fondo diferente o un icono diferente. Por ejemplo | |
// yo modificare el color de fondo de los TextView, y al ImageView le | |
// asignare una imagen diferente. | |
nombre.setText(arrayList.get(position).getNombre()); | |
fecha.setText(arrayList.get(position).getFecha()); | |
duracion.setText(arrayList.get(position).getDuracion()); | |
tipo.setText(arrayList.get(position).getTipo()); | |
iv_main.setImageResource(R.drawable.circle); | |
int color = Color.argb(255, random.nextInt(250), random.nextInt(250), random.nextInt(250)); | |
iv_main.setColorFilter(color); | |
// Se modifica la imagen del ImageView. La imagen que he colocado en una imagen | |
// de los recursos de Android. | |
tv_main.setImageResource(android.R.drawable.ic_dialog_email); | |
// El color de fondo de los TextView se modifican a gris | |
nombre.setBackgroundColor(Color.GRAY); | |
fecha.setBackgroundColor(Color.GRAY) | |
duracion.setBackgroundColor(Color.GRAY) | |
tipo.setBackgroundColor(Color.GRAY) | |
// Lo que te recomiendo es que modifiques el color de fondo del layout que | |
// utilizas como plantilla de los items. EL layout que contienes los | |
// TextView y los ImageView que modificas aqui. | |
} | |
return convertView; | |
} | |
} |
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 Item { | |
// variable que almacena el id que identifica las | |
// llamadas o los mensajes | |
private String idGroup; | |
private String nombre; | |
private String numero; | |
private String fecha; | |
private String duracion; | |
private String tipo; | |
public Item() { | |
this.idGroup = idGroup; | |
this.nombre = nombre; | |
this.numero = numero; | |
this.fecha = fecha; | |
this.duracion = duracion; | |
this.tipo = tipo; | |
} | |
public String getIdGroup () { | |
return idGroup; | |
} | |
public void setIdGroup (String idGroup) { | |
this.idGroup = idGroup; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public String getFecha() { | |
return fecha; | |
} | |
public void setFecha(String fecha) { | |
this.fecha = fecha; | |
} | |
public String getDuracion() { | |
return duracion; | |
} | |
public void setDuracion(String duracion) { | |
this.duracion = duracion; | |
} | |
public String getTipo() { | |
return tipo; | |
} | |
public void setTipo(String tipo) { | |
this.tipo = tipo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment