2010-06-14 78 views
9

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

這裏是我當前如何加載的ListView:

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

回答

7

以下是我如何解決問題。我從本地SQLite數據庫獲得了employee_ids和employee_names,然後同時創建了employeeNamesArray的ArrayList和employeeIdArray的ArrayList。因此,employeeIdArray [0]將與employeeNameArray [0]匹配,employeeIdArray [1]與employeeNameArray [1]匹配等。

一旦ArrayLists被創建,我將employeeNameArray送入ListView。

後來,在onListItemClick中,我檢索選定的ListView行的位置。這個'位置'會對ArrayList中的位置產生影響 - 因此,如果我在ListView中選擇第一行,則位置將爲零,並且employeeNameArray [0]與employeeIdArray [0]匹配。我從employeeIdArray中獲取冒充條目,並使用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位置......所以這是不正確的做法 – chhameed 2013-10-23 11:26:39

2

嗨克里斯,你已經在你的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(); 

    } 

,如果你想assing自己的ID使用setTag()

v.setTag("myownID"+position); 
+1

我希望能夠爲ListView中的每個位置分配一個id。我不想要自動分配的值。 – Chris 2010-06-15 13:55:59

1

你不能做到這一點與標準ArrayAdapter你需要擴展一個ArrayAdapter和覆蓋getItemId()方法,也許還hasStableIds()方法。

然後,您必須在hasStableIds方法中返回true,並在給予getItemId方法的位置爲該項目生成id。

0

願意花小時後,我發現這個最簡單的方法是覆蓋適配器的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); 
    } 
};