質問

ListViewがあります。 ListViewのアイテムがタップされると、サブビューがロードされます。 ListViewの各行にIDを割り当てたいので、そのIDをサブビューに渡すことができます。 ListViewの各行に特定のIDを割り当てるにはどうすればよいですか?

これが私が現在listViewをロードしている方法です:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mArrayList));
役に立ちましたか?

解決

これが私が問題を解決した方法です。 Local SQLiteデータベースからEmployee_IdsとEmployee_Namesを入手し、EmployeEENAMESARRAYの配列とEmployeeIdArrayのアレイリストを同時に作成しました。したがって、EmployeeIdArray [0]はEmployeEenamearray [0]と一致し、EmployeeDarray [1]はEmployeNamearray [1]などと一致します。

ArrayListsが作成されたら、equireNamearrayをListViewに供給しました。

その後、onlistitemclickで、選択したListView行の位置を再取得します。この「位置」はアレイリストの位置に腐敗します。したがって、ListViewで最初の行を選択すると、位置はゼロになり、EmployeNamearray [0]がEmployeeDarray [0]と一致します。 Employee IdarrayからCoroloatingエントリをつかみ、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  

    } 
}

他のヒント

こんにちは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を添付したい場合は、setag()を使用します

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帰属
所属していません StackOverflow
scroll top