且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为 Android ListView 中的一行分配 ID

更新时间:2022-06-21 22:57:57

这是我解决问题的方法.我从本地SQLite 数据库中获取了employee_ids 和employee_names,然后我同时创建了一个employeeNamesArray 的ArrayList 和employeeIdArray 的ArrayList.因此,employeeIdArray[0] 将与employeeNameArray[0] 匹配,employeeIdArray[1] 将与employeeNameArray[1] 匹配,等等.

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.

创建 ArrayLists 后,我将 employeeNameArray 输入到 ListView 中.

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

稍后,在 onListItemClick 中,我检索所选 ListView 行的位置.这个位置"将对应于 ArrayLists 中的位置 - 因此,如果我选择 ListView 中的第一行,则该位置将为零,并且employeeNameArray[0] 与employeeIdArray[0] 匹配.我从employeeIdArray 中获取cooloating 条目并使用putExtra 将其推送到下一个Activity.

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  

    } 
}