9

私はリストビューを持っています。ListView の項目がタップされると、SubView が読み込まれます。ListView の各行に ID を割り当てたいので、その ID を SubView に渡すことができます。ListView の各行に特定の ID を割り当てるにはどうすればよいですか?

現在ListViewをロードしている方法は次のとおりです。

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

4 に答える 4

9

これが私が問題を解決した方法です。ローカルの SQLite データベースから employee_ids と employee_names を取得し、employeeNamesArray の ArrayList と employeeIdArray の ArrayList を同時に作成しました。したがって、employeeIdArray[0] は employeeNameArray[0] と一致し、employeeIdArray[1] は employeeNameArray[1] と一致します。

ArrayLists が作成されたら、employeeNameArray を ListView に入力しました。

その後、onListItemClick で、選択した ListView 行の位置を取得します。この「位置」は ArrayLists の位置に対応します。したがって、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  

    } 
}
于 2010-06-16T21:32:28.820 に答える
2

標準のArrayAdapterではそれを行うことはできません。ArrayAdapterを拡張し、getItemId()メソッドとおそらくhasStableIds()メソッドを上書きする必要があります。

次に、hasStableIdsメソッドでtrueを返し、getItemIdメソッドに指定された位置にアイテムのIDを生成する必要があります。

于 2010-06-16T08:34:11.560 に答える
2

こんにちは Chris さん、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);
于 2010-06-14T22:36:36.823 に答える