Custom SimpleAdapter sólo muestra texto de ejemplo Android

Antes de hacer mi propio objeto SimpleAdapter porque quería cambiar el color de las filas, estaba usando SimpleAdapter (…). Ahora que estoy usando mi propio SimpleAdapter personalizado, el color de la fila está cambiando, pero mi texto no se está actualizando. He llamado adaptador.notifyDataSetChanged (), pero todavía está mostrando solamente el texto de la muestra – "TextView". Como he dicho, todo funcionaba bien cuando no creaba mi propio adaptador. Sospecho que podría tener algo que ver con la orden que estoy inicializando cosas:

public class AddScreen extends Activity implements OnClickListener, OnItemClickListener, OnItemLongClickListener { SimpleAdapter adapter; List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>(); ListView listthings; int[] to; String[] from; public void onCreate(Bundle savedInstanceState) { ... listthings = (ListView) findViewById(R.id.listthings); from = new String[] { "row_1", "row_2" }; to = new int[] { R.id.row1, R.id.row2 }; adapter = new Adapter(this, painItems, R.layout.mylistlayout, from, to); listthings.setAdapter(adapter); ... } public class Adapter extends SimpleAdapter{ HashMap<String, String> map = new HashMap<String, String>(); public Adapter(Context context, List<? extends Map<String, String>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } @Override public View getView(int position, View convertView, ViewGroup parent){ View row = convertView; if (row == null) { LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = mInflater.inflate(R.layout.mylistlayout, parent, false); } row.setBackgroundColor(0xFF0000FF); TextView rw1 = (TextView)findViewById(R.id.row1); // TextView rw2 = (TextView)findViewById(R.id.row2); rw1.setText(map.get(position)); return row; } } // to add the item, put it in the map, and add the map into the list private void addItem() { HashMap<String, String> map = new HashMap<String, String>(); map.put("row_1", row1); map.put("row_2", row2); map.put("row_3", painLevelString); map.put("row_4", painLocation); map.put("row_5", timeOfPainString); map.put("row_6",textTreatmentString); painItems.add(map); adapter.notifyDataSetChanged(); } 

EDIT: Código añadido

Así es como obtengo los datos de la intención (onActivityResult ()), colocados antes del código addItem:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == 1) { row1 = data.getStringExtra("com.painLogger.row1"); row2 = data.getStringExtra("com.painLogger.row2"); painLevelString = data.getStringExtra("com.painLogger.painLevel"); painLocation = data.getStringExtra("painLocation"); timeOfPainString = data.getStringExtra("com.painLogger.painTime"); textTreatmentString = data .getStringExtra("com.painLogger.treatment"); addItem(); } } 

* También, en caso de que esto sea relevante, el orden de colocación es: onCreate () -> custom Adapter class -> onActivityResult () -> addItem () * **

Aquí hay una captura de pantalla de lo que parece. Los dos campos TextView en cada elemento deben rellenarse con información (que fueron, hasta que hice esto).

Si trabajó anteriormente con sólo usar new SimpleAdapter(...) entonces en su getView(...) cambiar la primera línea a esto:

 View row = super.getView(position, convertView, parent); 

Y ver si eso es lo que esperas. Saque el material LayoutInflater también.

En getView() , acerca de dónde está configurando el fondo de la fila, también debe establecer el texto para TextView .

Llamar notifyDataSetChanged() , no establece automagically sus textos a la derecha, sólo hace que el ListView para volver a dibujar las filas visibles con los nuevos datos … prácticamente llamada getView() para cada fila que necesita una actualización.

También sugiero configurar el color de fondo del archivo mylistlayout.xml , y si la función getView () comienza a tomar unos pocos findViewByID 's, también debe considerar el uso de un "titular de la vista" enfoque como en este ejemplo: http: // developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

getView() establecer el texto en getView() . Me gusta esto:

 public View getView(int position, View convertView, ViewGroup parent){ TextView text; if (convertView == null) { LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = mInflater.inflate(R.layout.mylistlayout, parent, false); text = (TextView) convertView.findViewById(R.id.more_list_text); } convertView.setBackgroundColor(0xFF0000FF); text.setText(map.get(position)); return convertView; } 

Además, y esto es MUY importante – almacenar el mapa como una variable miembro de la SimpleAdapter
es decir, ponga esta línea en la parte superior de su definición de objeto:

 HashMap<String, String> map = new HashMap<String, String>(); 
  • Mejor comprensión del ViewBinder de SimpleAdapter
  • ¿Cómo mostrar imágenes en imageview en un adaptador simple?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.