How to call a method in the fragment through the listview adapter

1

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

The button has to delete the listview item

I've already done a method within the onactivitycreated, but it gives error of "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 - notifications.xml list

<?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>
    
asked by anonymous 30.05.2017 / 02:21

1 answer

0

You should create an Adapter for your ListView, which inherits from the BaseAdapter class:

public class MeuAdapter extends BaseAdapter

Now within the getView () method, 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
        }
    });
    
25.07.2017 / 16:33