簡體   English   中英

為Android ListView中的行分配ID

[英]Assigning ID to a Row in an Android ListView

我有一個ListView。 當點擊ListView上的項目時,它會加載一個SubView。 我想為ListView的每一行分配一個ID,所以我可以將該ID傳遞給SubView。 如何為ListView中的每一行分配特定ID?

這是我當前加載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]等。

創建ArrayLists后,我將employeeNameArray輸入ListView。

稍后,在onListItemClick中,我檢索所選ListView行的位置。 這個'position'將與ArrayLists中的位置相對應 - 因此,如果我選擇ListView中的第一行,則位置將為零,employeeNameArray [0]與employeeIdArray [0]匹配。 我從employeeIdArray中獲取了coroloating條目,並使用putExtra將其推送到下一個Activity。

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  

    } 
}

嗨,你已經在listView中擁有了位置id,實現了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();

   }

如果你想要自己的id使用setTag()

v.setTag("myownID"+position);

您無法使用標准ArrayAdapter執行此操作您需要擴展ArrayAdapter並覆蓋getItemId()方法,也可能使用hasStableIds()方法。

然后,您必須在hasStableIds方法中返回true,並在為getItemId方法指定的位置生成項目的id。

花了好幾個小時后,我找到的最簡單的方法是覆蓋適配器的bindView並在項目上設置包含行的_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);
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM