删除时获取列表视图项值(特定文本视图)

时间:2016-03-31 08:09:07

标签: android listview http-delete

我正在创建一个CMS应用程序,我想要删除一个项目,因为我需要我在listview项目中隐藏的ID。当我拿着一个listview项目时会弹出一个我点击的删除按钮,这会让我看到这段代码:

@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);
    }
}

自定义列表视图:

My custom listview

当按住某个项目时,会显示该代码。 (Verwijderen ==删除) When hold clicking on an item this appears wich calls the code.

自定义适配器的自定义行XML:

<?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>

2 个答案:

答案 0 :(得分:1)

试试这个

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);
    }
}

答案 1 :(得分:0)

您需要获取所选项目位置(来自列表视图)

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

现在使用index从数据集中获取项目 例如,假设数据集ArrayListArray名为myItems

int itemId = myItems.get(index).getId();
//call API using itemId as parameter