简体   繁体   中英

Get _id and position item after click button on listView - Android

Actually I have update item option in database when I click its position on listView . Code in onCreate method:

AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> listViewOrganizacje, View view, int position, long id) {
        Intent intent = new Intent(BazaDanych.this, BazaDanychAktualizacja.class);
        intent.putExtra(BazaDanychAktualizacja.EXTRA_ORGANIZACJA_ID, (int) id);
        startActivity(intent);
    }
};
listViewSeriale.setOnItemClickListener(itemClickListener);

And it work fine. But I added "update" button for my items on listView and I would like to use it and use the same code like above but I don't know how to use it with button on list. My update button preferences in.xml:

<Button
    android:id="@+id/updateButton"
    style="@android:style/Widget.Holo.Button"
    android:layout_width="36dp"
    android:layout_height="25dp"
    android:layout_weight="1"
    android:background="#000000"
    android:text="update"
    android:textAlignment="center"
    android:textAllCaps="false"
    android:textSize="15dp"
    android:onClick="clickUpdateButton"
    />

And here is a method where I try to use it:

public void clickUpdateButton(View view) {

    //        AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> listViewOrganizacje, View view, int position, long id) {
//                Intent intent = new Intent(BazaDanych.this, BazaDanychAktualizacja.class);
//                intent.putExtra(BazaDanychAktualizacja.EXTRA_ORGANIZACJA_ID, (int) id);
//                startActivity(intent);
//            }
//        };
//        listViewSeriale.setOnItemClickListener(itemClickListener);

}

Problem solved.

Here is a solution code in clickUpdateButton method:

int position = listViewSeriale.getPositionForView((View)view.getParent());

Cursor c = (Cursor) listAdapter.getItem(position);
String id = cursor.getString(0); // column number in database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM