在Android ListView中为行分配ID [英] Assigning ID to a Row in an Android ListView

查看:83
本文介绍了在Android ListView中为行分配ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView.轻按ListView上的项目时,它将加载一个SubView.我想为ListView的每一行分配一个ID,因此可以将该ID传递给SubView.如何为ListView中的每一行分配一个特定的ID?

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. How do I assign a specific ID to each row in the ListView?

这是我当前加载ListView的方式:

Here is how I am currently loading the ListView:

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

推荐答案

这是我解决问题的方法.我从本地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.

一旦创建了ArrayList,我就将employeeNameArray输入到ListView中.

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

后来,在onListItemClick中,我检索了所选ListView行的位置.该位置"将对应于ArrayLists中的位置-因此,如果我在ListView中选择第一行,该位置将为零,而employeeNameArray [0]与employeeIdArray [0]相匹配.我从employeeIdArray抓取了相应的条目,然后使用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  

    } 
}

这篇关于在Android ListView中为行分配ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆