如何设置ListView的ImageButton的Listener

时间:2016-05-17 12:28:11

标签: android listview

我在Android应用程序上有一个DialogFragment。从这个Dialog,我可以在一个ListView上添加任何项目。现在我想显示一个删除按钮,从列表视图中删除此项目。

所以这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:paddingTop="5dp"
    android:clickable="true">

    <TextView
        android:id="@+id/description"
        android:textColor="@color/black"
        android:layout_width="250dp"
        android:layout_height="@dimen/list_view_row_height"
        android:textSize="@dimen/table_row_text_size"
        />
    <ImageButton
        android:id="@+id/deleteButton"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/description"
        android:src="@drawable/delete"/>

</RelativeLayout>

这是我的自定义适配器ListView

    public class AgentSelectedListViewAdapter extends ArrayAdapter<AlertValueSet> {

        Context context;
        int layoutResourceId;
        AlertValueSet data[] = null;

        public AgentSelectedListViewAdapter(Context context, List<AlertValueSet> users) {
            super(context, 0, users);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            AlertValueSet user = getItem(position);
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.alert_agent_selected_list_row, parent, false);
            }
            // Lookup view for data population
            TextView tvName = (TextView) convertView.findViewById(R.id.description);
            // Populate the data into the template view using the data object
            tvName.setText(user.getDisplayName());
            // Return the completed view to render on screen
            return convertView;
        }
    }

This is the code of DialogFragment

public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        v = getActivity().getLayoutInflater().inflate(R.layout.alert_insert_dialog, null);
        builder.setView(v);
        builder.setTitle("Insert Alerts");
        builder.setCancelable(false);

        //spinner status
        TextView textAgent = (TextView) v.findViewById(R.id.labelAgent);
        textAgent.setOnClickListener(new AgentClickListener());

        //popolo la lista di Agent selezionati inizialmente sarà 0
        ListView recyclerView = (ListView) v.findViewById(R.id.listOfAgent);
        adapterListAgent = new AgentSelectedListViewAdapter(v.getContext(),listValueSet_Selected_Agent);
        recyclerView.setAdapter(adapterListAgent);
        return builder.create();
    }

现在,我可以设置删除图像的监听器的每个项目吗?

1 个答案:

答案 0 :(得分:0)

这里的界面有助于从列表项更新列表, 将其放在列表适配器

public static interface ProductsCheckoutAdapterCallbacks {
      void onremoveCartButtonClicked(ImageView removebutton);
}