Assigning ID to a Row in an Android ListView

后端 未结 4 1270
天涯浪人
天涯浪人 2020-12-28 22:20

I have a ListView. When an item on the ListView is tapped, it loads a SubView. I want to assign an ID to each row of the ListView, so I can pass that ID along to the SubView

相关标签:
4条回答
  • 2020-12-28 22:39

    Here's how I solved the problem. I got the employee_ids and employee_names from the local SQLite Database, then I created an ArrayList of employeeNamesArray and an ArrayList of employeeIdArray at the same time. Thus, the employeeIdArray[0] would match with employeeNameArray[0], employeeIdArray[1] would match with employeeNameArray[1], etc.

    Once the ArrayLists were created, I fed employeeNameArray into the ListView.

    Later, in onListItemClick, I retreive the position of the selected ListView row. This 'position' will corrospond to the position in the ArrayLists - thus, if I select the first row in the ListView, the position will be zero, and employeeNameArray[0] matches with employeeIdArray[0]. I grab the coroloating entry from employeeIdArray and push that to the next Activity by using 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  
    
        } 
    }
    
    0 讨论(0)
  • 2020-12-28 22:42

    You can't do that with a standard ArrayAdapter You need to extend the ArrayAdapter and overwrite the getItemId() method and maybe also the hasStableIds() method.

    You then have to return true in the hasStableIds method and generate your id for the item at the position that is given to your getItemId method.

    0 讨论(0)
  • 2020-12-28 22:47

    After spending hours on this, this easiest way I found was to override bindView of the adapter and set a tag value containing the row's _id on the item - in my case, it was a button in the ListView row.

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.note, cursor, fromColumns, toViews, 0) {
    
        @Override
        // add the _id field value to the button's tag
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);
            Integer index = cursor.getColumnIndex("_id");
            Integer row_id = cursor.getInt(index);
            Button button = (Button) view.findViewById(R.id.button_delete_record);
            button.setTag(row_id);
        }
    };
    
    0 讨论(0)
  • 2020-12-28 22:57

    Hi Chris you already have the position id in your listView, implement the onListItemClick() function.

        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();
    
       }
    

    if you want assing your own id use setTag()

    v.setTag("myownID"+position);
    
    0 讨论(0)
提交回复
热议问题