How to call a method in Fragment via listview Adapter

Asked

Viewed 453 times

1

I have a fragment that has a listview inside it and inside lisview has a button.

The button has to delete the listview item

I have already made a method within onactivitycreated, but gives error "Java lang null Pointer"

Fragment code

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    minha_view = inflater.inflate(R.layout.lista_notificacoes, container, false);
    return minha_view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    recupera_tarefa();

    botao_deletar = (ImageView) getView().findViewById(R.id.botao_deletar);

    botao_deletar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(),"teste de mensagem",Toast.LENGTH_SHORT).show();
        }
    });
}

layout code - list notifications.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#000000"
    android:dividerHeight="2dp"
    android:id="@+id/list_notify"
    >
</ListView>
</FrameLayout

code of Listview items being added at runtime after the user registers an item.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:id="@+id/numero_notificacao"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="center"
    android:background="@drawable/circle"
    android:fontFamily="sans-serif"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:text="texto"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginBottom="4dp"

    />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">
    <TextView
        android:id="@+id/texto_notificacao_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:textColor="#000000"
        android:text="fedsfse"
        android:maxLines="1"
        android:ellipsize="end"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"/>
    <TextView
        android:id="@+id/texto_hora_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="TextVie"
        android:layout_marginLeft="8dp" />
</LinearLayout>

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/delete"
    android:id="@+id/botao_deletar"
    android:layout_gravity="center"
    android:layout_marginRight="8dp"/>
</LinearLayout>

1 answer

0

You must create an Adapter for your Listview, which you inherit from the Basedapter class:

public class MeuAdapter extends BaseAdapter

Now within the method getView(), you will instantiate a view for each item in your Listview.

LayoutInflater inflater = LayoutInflater.from(context);
    View view = convertView;

    if(view == null){
        view = inflater.inflate(R.layout.item_lista, parent, false);
    }

    //Inicializando Componentes
    ImageView botao_deletar = (ImageView) view.findViewById(R.id.botao_deletar);

Finally, add the event to your Imageview in the Adapter class itself.

botao_deletar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Seu código
        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.