单击listview上的一个项目时获取主键

时间:2017-12-28 17:00:33

标签: android listview

如果我使用我的表的主键填充我的listview,我可以使用getItemAtPosition获取此主键,然后它可以正常工作。

问题是我不想使用primarykey来填充de listview,而是想要使用我表中的其他字段。这样做,当我使用getItemAtPosition命令时,因为它不是unic我不能用它来选择我的寄存器。

我考虑过使用getItemIdAtPosition,但我没有达成任何解决方案。

public void populateListView() {
        //get the data and append to the list
        Cursor data = db.getAllDataFillup(selectedID);
        ArrayList<String> listData2 = new ArrayList<>();


        while (data.moveToNext()) {
            //listData2.add("FILLUP_ID: " + data.getString(0) + "    FILLUP_VEHICLE_ID: " + data.getString(1));
            //listData2.add(data.getString(7) + " " + data.getString(8) + " " + data.getString(2));
            listData2.add(data.getString(3));
            //listData2.add(data.getString(2));

        }



        //create the list adapter and set the adapter

        ListAdapter adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData2);
        list_fillup.setAdapter(adapter2);


        //set onItemClickListener to the listView
        list_fillup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int fillupID = Integer.parseInt(adapterView.getItemAtPosition(i).toString());

                long position = adapterView.getSelectedItemId();
                //long a = list_fillup.get(codigoDoObjeto).getCodigoIdOuPKQualquer();



                toastMessage("position:  " + position);
                //toastMessage("fillupPosition:  " + fillupPosition);
                //long fillupPosition = adapterView.getItemIdAtPosition(i);

                Log.d(TAG, "onItemClick: You Clicked on " + fillupID);


                Cursor data = db.getDataTableFillup(fillupID);//get the data associated with that fillupID

                fillupID = -1;
                while (data.moveToNext()) {
                    fillupID = data.getInt(0);
                    vehicleID = data.getInt(1);
                    fillupDate = data.getString(2);
                    odometer = data.getLong(3);
                    kmDriven = data.getLong(4);
                    liters = data.getLong(5);
                    consumption = data.getLong(6);
                    label = data.getString(7);
                    sequence = data.getInt(8);
                }
                if (fillupID > -1) {
                    Log.d(TAG, "onItemClick: The ID is: " + fillupID);
                    Intent screenVehicle = new Intent(Vehicle_painel.this, Fillup_edit.class);
                    screenVehicle.putExtra("fillupID", fillupID);
                    screenVehicle.putExtra("vehicleID", vehicleID);
                    screenVehicle.putExtra("vehicleName", selectedName);
                    screenVehicle.putExtra("date", fillupDate);
                    screenVehicle.putExtra("odometer", odometer);
                    screenVehicle.putExtra("kmDriven", kmDriven);
                    screenVehicle.putExtra("liters", liters);
                    screenVehicle.putExtra("consumption", consumption);
                    screenVehicle.putExtra("label", label);
                    screenVehicle.putExtra("sequence", sequence);

                    //toastMessage("fillupPosition:  " + fillupPosition);
                    startActivity(screenVehicle);

                } else {
                    toastMessage("fillupID = " + fillupID);
                    //db.deleteAllFillup(selectedID);
                    //toastMessage("No ID associated with that name hahaha");
                }

1 个答案:

答案 0 :(得分:1)

最好的办法是创建一个自定义类来保存数据。这样,您不再只是从适配器中获取简单的String值。您的ArrayList类似于:

ArrayList<YourCustomClass> listData2 ...

创建一个自定义类&#34; YourCustomClass&#34; (称之为你喜欢的)。它可能看起来像:

 public class YourCustomClass {

    private long itemId = 0;
    private String itemName;
    private String itemDescription;

    public YourCustomClass(){
    }


    public void setItemId(long id){ this.itemId = id; }
    public void setItemName(String itemName){ this.itemName = itemName; }
    public void setItemDescription(String itemDescription){ this.itemDescription = itemDescription; }

    public long getItemId() { return this.itemId; }
    public String getItemName(){ return this.itemName; }
    public String getItemDescription(){ return this.itemDescription; }
}

现在,在您的onItemClick方法中,获取ID和其他数据,如下所示:

YourCustomClass data = (YourCustomClass) adapterView.getItemAtPosition(i);
long orderId = data.getItemId();
String name = data.getItemName();

您需要一个自定义适配器来填充ListView数据。

你也可以看看这个答案。它显示了如何更改ListView项目的背景颜色,还显示了如何为ListView实现自定义适配器的更多详细信息。

How to set background color for each item listview depanding on variable value

相关问题