سؤال

لدي ListView.عندما يكون عنصر على ListView هو استغلالها, تحميل SubView.أريد أن تعيين معرف لكل صف من ListView, حتى أتمكن من تمرير هذا الرقم على طول إلى العرض الفرعي.كيف يمكنني تعيين هوية محددة إلى كل صف في ListView?

هنا هو كيف أنا حاليا تحميل ListView:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mArrayList));
هل كانت مفيدة؟

المحلول

وهنا كيف أنا حل المشكلة.حصلت على employee_ids و employee_names من قاعدة بيانات SQLite, ثم أنشأت ArrayList employeeNamesArray و ArrayList employeeIdArray في نفس الوقت.وهكذا employeeIdArray[0] أن تتطابق مع employeeNameArray[0], employeeIdArray[1] أن تتطابق مع employeeNameArray[1] ، إلخ.

مرة واحدة ArrayLists تم إنشاؤها ، ارتويت employeeNameArray في ListView.

في وقت لاحق في onListItemClick أنا لإستعادة موقف اختيار ListView الصف.هذا 'موقف' سوف corrospond إلى الموقف في ArrayLists - وبالتالي ، إذا كنت تحديد الصف الأول في ListView تكون صفر ، employeeNameArray[0] مباريات مع employeeIdArray[0].أنا الاستيلاء على coroloating دخول من 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  

    } 
}

نصائح أخرى

مرحبًا Chris ، لديك بالفعل معرف الموضع في ListView الخاص بك ، قم بتنفيذ وظيفة 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();

   }

إذا كنت ترغب في الحصول على معرفك الخاص ، فاستخدم Settag ()

v.setTag("myownID"+position);

لا يمكنك فعل ذلك بمعايير ArrayAdapter تحتاج إلى تمديد ArrayAdapter والكتابة فوق getitemid () الطريقة وربما أيضا hasstableids () طريقة.

عليك بعد ذلك العودة بشكل صحيح في طريقة HasStableIDs وإنشاء معرفك للعنصر في الموضع الذي يتم تقديمه إلى طريقة GetItemId الخاصة بك.

بعد قضاء ساعات في هذا الأمر ، كانت هذه الأسهل التي وجدتها هي تجاوز 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