Asignación de ID a una fila en un ListView de Android

Tengo un ListView. Cuando se pulsa un elemento en el ListView, carga un SubView. Quiero asignar una identificación a cada fila de ListView, así que puedo pasar esa identificación junto a la SubView. ¿Cómo asigno un ID específico a cada fila en el ListView?

Así es como estoy cargando el ListView:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mArrayList)); 

He aquí cómo resolví el problema. Conseguí employee_ids y employee_names de la base de datos local de SQLite, después creé un ArrayList de employeeNamesArray y un ArrayList de employeeIdArray al mismo tiempo. Por lo tanto, el employeeIdArray [0] sería igual a employeeNameArray [0], employeeIdArray [1] sería igual a employeeNameArray [1], etc.

Una vez que los ArrayLists fueron creados, yo fed employeeNameArray en el ListView.

Más tarde, en onListItemClick, recupero la posición de la fila ListView seleccionada. Esta 'posición' corregirá a la posición en el ArrayLists – por lo tanto, si selecciono la primera fila en el ListView, la posición será cero, y employeeNameArray [0] coincide con employeeIdArray [0]. Agarrar la entrada coroloating de employeeIdArray y empujar que a la siguiente actividad mediante putExtra.

 public class MyFirstDatabase extends ListActivity { ArrayList<String> employeeIdArray = new ArrayList<String>(); // List of EmployeeIDs /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Open the database SQLiteDatabase db; db = openOrCreateDatabase("mydb.db",SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setVersion(1); db.setLocale(Locale.getDefault()); db.setLockingEnabled(true); // Query the database Cursor cur = db.query("employee", null, null, null, null, null, "employee_lastname"); cur.moveToFirst(); // move to the begin of the db results ArrayList<String> employeeNameArray = new ArrayList<String>(); // Initialize mArrayList while (cur.isAfterLast() == false) { employeeNameArray.add(cur.getString(1)); // add the employee name to the nameArray employeeIdArray.add(cur.getString(0)); // add the employee id to the idArray cur.moveToNext(); // move to the next result set in the cursor } cur.close(); // close the cursor // put the nameArray into the ListView setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item,employeeNameArray)); ListView lv = getListView(); lv.setTextFilterEnabled(true); } protected void onListItemClick(ListView l, View v, final int position, long id) { super.onListItemClick(l, v, position, id); Intent myIntent = new Intent(this, SubView.class); // when a row is tapped, load SubView.class Integer selectionID = Integer.parseInt(employeeIdArray.get(position)); // get the value from employeIdArray which corrosponds to the 'position' of the selected row myIntent.putExtra("RowID", selectionID); // add selectionID to the Intent startActivityForResult(myIntent, 0); // display SubView.class } } 

Hola Chris, ya tienes el id de posición en tu listView, implementa la función onListItemClick ().

  protected void onListItemClick(ListView l, View v, final int position, long id) { super.onListItemClick(l, v, position, id); Toast.makeText(this, "my id to pass along the subview is " + position,Toast.LENGTH_LONG).show(); } 

Si quieres contar tu propio id use setTag ()

 v.setTag("myownID"+position); 

No puede hacerlo con un ArrayAdapter estándar . Necesita extender el ArrayAdapter y sobrescribir el método getItemId () y tal vez también el método hasStableIds () .

A continuación, tiene que devolver true en el método hasStableIds y generar su id para el elemento en la posición que se da a su método getItemId.

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.