代码之家  ›  专栏  ›  技术社区  ›  Chris

为Android列表视图中的行分配ID

  •  8
  • Chris  · 技术社区  · 14 年前

    下面是我当前加载ListView的方式:

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mArrayList));
    
    4 回复  |  直到 13 年前
        1
  •  9
  •   Chris    14 年前

    我是这样解决这个问题的。我从本地SQLite数据库中获取了employee\ id和employee\ name,然后同时创建了employeeNamesArray的ArrayList和employeeIdArray的ArrayList。因此,employeedArray[0]将与employeeNameArray[0]匹配,employeedArray[1]将与employeeNameArray[1]匹配,等等。

    稍后,在onListItemClick中,我检索所选ListView行的位置。此“position”将与ArrayList中的位置对应-因此,如果我在ListView中选择第一行,则该位置将为零,并且employeeNameArray[0]与employeeIdArray[0]匹配。我从employeeIdArray中抓取coroloating条目,并使用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  
    
        } 
    }
    
        2
  •  2
  •   Jorgesys    14 年前

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

    如果您想分配自己的id,请使用setTag()

    v.setTag("myownID"+position);
    
        3
  •  2
  •   Janusz Daniel Rindt    14 年前

    你不能用标准来做 ArrayAdapter getItemId() 方法,也许还有 hasStableIds()

    然后,您必须在hasStableIds方法中返回true,并在给定给getItemId方法的位置生成项的id。

        4
  •  0
  •   siliconrockstar    7 年前

    在这上面花了几个小时之后,我发现最简单的方法是重写适配器的bindView,并在项目上设置一个包含行的\u id的标记值——在我的例子中,它是ListView行中的一个按钮。

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