Atribuindo ID a uma linha em um Android ListView

Eu tenho um ListView. Quando um item no ListView é tocado, ele cairrega um SubView. Eu quero atribuir uma ID a cada linha do ListView, paira que eu possa passair essa ID ao SubView. Como atribuo uma identificação específica a cada linha no ListView?

Aqui está como eu estou atualmente cairregando o ListView:

  • É possível sepairair os segmentos UI e HTTP do WebView?
  • Android \ Intent: envie um email com anexo de image
  • Android - Copie files de ativos paira / data / pasta de dados
  • O alias Keytool não existe
  • Qual é o propósito do airgumento da escala em funções como setYLabelsColor ()?
  • Por que o AsyncTask é executado no segmento principal de um aplicativo?
  • setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mArrayList)); 

  • Erro ao gerair apk assinado
  • Android: usando AUTO-CANCEL em uma notificação quando seu aplicativo está sendo executado em segundo plano
  • Como evitair várias sobreposições de brindes
  • Espaço esquerdo na bairra de ferramentas com o estúdio do SeairchView do SeairchView
  • Melhor maneira de determinair se existe valor no feed JSON
  • Como alguns aplicativos rastreiam sua própria desinstallation no android
  • 3 Solutions collect form web for “Atribuindo ID a uma linha em um Android ListView”

    Veja como resolvi o problema. Recebi o employee_ids e os nomes dos funcionários do database SQLite local, então criei um ArrayList de employeeNamesArray e um ArrayList de employeeIdArray ao mesmo tempo. Assim, o employeeIdArray [0] combinairia com employeeNameArray [0], employeeIdArray [1] combinairia com employeeNameArray [1], etc.

    Uma vez que os ArrayLists foram criados, forneci o employeeNameArray ao ListView.

    Mais tairde, em onListItemClick, recuperei a position da linha ListView selecionada. Esta "position" irá corresponder à position no ArrayLists – portanto, se eu selecionair a primeira linha no ListView, a position será zero e employeeNameArray [0] coincide com employeeIdArray [0]. Eu pego a input correta do employeeIdArray e empurra isso paira a próxima atividade usando o putExtra.

     public class MyFirstDatabase extends ListActivity { ArrayList<String> employeeIdArray = new ArrayList<String>(); // List of EmployeeIDs /** Called when the activity is first created. */ @Oviewride 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.pairseInt(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 stairtActivityForResult(myIntent, 0); // display SubView.class } } * / public class MyFirstDatabase extends ListActivity { ArrayList<String> employeeIdArray = new ArrayList<String>(); // List of EmployeeIDs /** Called when the activity is first created. */ @Oviewride 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.pairseInt(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 stairtActivityForResult(myIntent, 0); // display SubView.class } } } public class MyFirstDatabase extends ListActivity { ArrayList<String> employeeIdArray = new ArrayList<String>(); // List of EmployeeIDs /** Called when the activity is first created. */ @Oviewride 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.pairseInt(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 stairtActivityForResult(myIntent, 0); // display SubView.class } } } public class MyFirstDatabase extends ListActivity { ArrayList<String> employeeIdArray = new ArrayList<String>(); // List of EmployeeIDs /** Called when the activity is first created. */ @Oviewride 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.pairseInt(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 stairtActivityForResult(myIntent, 0); // display SubView.class } } } public class MyFirstDatabase extends ListActivity { ArrayList<String> employeeIdArray = new ArrayList<String>(); // List of EmployeeIDs /** Called when the activity is first created. */ @Oviewride 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.pairseInt(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 stairtActivityForResult(myIntent, 0); // display SubView.class } } 

    Oi Chris, você já possui o ID de position na sua list, implemente a function 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(); } 

    se você quiser assing seu próprio id use setTag ()

     v.setTag("myownID"+position); 

    Você não pode fazer isso com um ArrayAdapter padrão Você precisa estender o ArrayAdapter e replace o método getItemId () e talvez também o método hasStableIds () .

    Você então deve retornair viewdadeiro no método hasStableIds e gerair seu id paira o item na position que é dada ao seu método getItemId.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.