Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
tags editadas
Link
ramaral
  • 44,7mil
  • 14
  • 60
  • 110
Fonte Link
Demetrius
  • 105
  • 1
  • 12

Como chamar um método no fragment através do adapter do listview

Eu tenho um fragmente que tem um listView dentro dele e dentro do lisview tem um botão.

O botão tem que deletar o item do listview

Eu já fiz um método dentro do onactivitycreated, mas dá erro de "Java lang null pointer"

código do fragment

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

código do layout - lista notificações.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

código dos itens do Listview que está sendo adicionado em tempo de execução após o usuário cadastrar um 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>