0

I am creating a CMS application and I want to delete an item, for that I need the ID that I have hidden in the listview item. When I hold a listview item it pops up with a delete button which I click, that brings me to this piece of code:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
        case R.id.delete:

            new HttpDelete("http://test.soundwave.drieo.nl/api/content/"+ text + apikey);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

Custom listview:

My custom listview

When hold clicking on an item this appears wich calls the code. (Verwijderen == delete) When hold clicking on an item this appears wich calls the code.

Custom Row XML for custom adapter:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="6dip"
    android:contentDescription="@string/TODO"/>

<TextView
    android:id="@+id/secondLine"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_toEndOf="@id/icon"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="@string/description"
    android:textSize="12sp"
    android:visibility="visible"/>

<TextView
    android:id="@+id/firstLine"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLine"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toEndOf="@id/icon"
    android:gravity="center_vertical"
    android:text="@string/Example"
    android:textSize="16sp" />

<TextView
    android:id="@+id/tbid"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="@string/ID"
    android:textSize="12sp"
    android:layout_alignTop="@+id/secondLine"
    android:layout_toEndOf="@+id/icon"
    android:layout_marginStart="68dp"
    android:visibility="invisible"/>

</RelativeLayout>
  • 1
    So what is the issue? What you need please elaborate? – Alok Gupta Mar 31 '16 at 08:11
  • Refer http://stackoverflow.com/questions/14328596/retrieving-text-from-a-listview-item-with-oncontextitemselected – Vijay Mar 31 '16 at 08:19
  • @Vijay i tried that page but didnt understand/get it to work. –  Mar 31 '16 at 08:24
  • @AlokGupta im sorry if i didnt say this properly, i need to get the ID aka text that is in the link on HTTPDelete, so i can delete that specific item. Il edit in some pictures to make it more clear –  Mar 31 '16 at 08:24
  • @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); int index = info.position; //use this index to get current selected data from adapter } – Vijay Mar 31 '16 at 08:40
  • @Vijay i added it in, but how do i use this to get the text from the textview called id and put tht into a string wich i can use in the link? I am new to adnroid so i dont have the best understanding of this yet. –  Mar 31 '16 at 08:52
  • Can you show you adapter item view layout xml? – Vijay Mar 31 '16 at 08:54
  • @Vijay Added it in, hope this is what you mean –  Mar 31 '16 at 08:59

2 Answers2

1

Try this

public boolean onContextItemSelected(MenuItem item){
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

    switch(item.getItemId()) {
    case R.id.delete:    
Toast.makeText(MainActivity.this, ((TextView) info.targetView.findViewById(R.id.tbid)).getText().toString(),Toast.LENGTH_SHORT).show();

    return true;
    default:
        return super.onContextItemSelected(item);
    }
}
Vijay
  • 563
  • 1
  • 6
  • 18
0

you need to get the selected item position (from listview)

public boolean onContextItemSelected(MenuItem item) {
    //...codes 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
    //... other codes
}

now use index to get the item from the data set ex, assume data set ArrayList or Array named myItems:

int itemId = myItems.get(index).getId();
//call API using itemId as parameter
Yazan
  • 6,074
  • 1
  • 19
  • 33