How to pass data from one Fragment to another (upadte Sqlite)?

Asked

Viewed 496 times

0

I’m developing an app with Sqlite, I want to take the dice of a ListView (where I am displaying the saved information) and want to play for a Fragment to enable the user to edit it.

I’ve tried several ways, but I can’t pass the data :/

package com.example.android.projetotccpvp;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class ShowPvpFragment extends Fragment {
    private SQLiteDatabase database;
    private CursorAdapter cursorAdapter;
    private ListView listView;
    CriaBanco helper;

    private static final String campos[] = {"assunto","comoestou","comodeveria","oquefazer",
    "comofazer","atequando","_id"};

    public ShowPvpFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_show_pvp, container, false);

        listView = (ListView)view.findViewById(R.id.listExibe);
        //Cria instancia da classe Cria Banco que é responsável pelo Banco de Dados
        helper = new CriaBanco(ShowPvpFragment.this.getActivity());
        // Executa as rotinas internar para poder utilizar o banco
        database = helper.getWritableDatabase();

         final Cursor dados = database.query("pvp",campos,null,null,null,null,null);

        if(dados.getCount() > 0){
            // Cria o cursor que será exibido na tela e junto dele os dados
            cursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.exibe_pvp_tabela,dados,
                    campos,new int[]{R.id.mostraAssunto,R.id.mostraComoEstou,R.id.mostraComoDeveria,
            R.id.mostraoQueFazer,R.id.mostraComoFazer,R.id.mostraAteQuando});

            listView.setAdapter(cursorAdapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String codigo;
                    dados.moveToPosition(position);
                    codigo = dados.getString(dados.getColumnIndexOrThrow(CriaBanco.ID));
                    Fragment fragment = new EditPvpFragment();
// Aqui foi onde eu tentei pegar os dados para passar para a outra
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    Bundle args = new Bundle();
                    args.putString("codigo",codigo);
                    fragment.setArguments(args);
                    fragmentTransaction.replace(R.id.contentMain,fragment);
                    fragmentTransaction.commit();


                    /*Intent intent = new Intent(getActivity(),EditPvpFragment.class);
                    intent.putExtra("codigo",codigo);
                    startActivity(intent);*/
                }
            });
        }
        else{
            Toast.makeText(getActivity(),"Nenhum registro encontrado!",Toast.LENGTH_LONG).show();
        }
        return view;
    }
}

This is the class where I want to pass the dice

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class EditPvpFragment extends Fragment {
    EditText comoEstou, comoDeveria, oQueFazer, comoFazer;
    TextView quandoFazer, assunto;
    Button salvarEdicao;
    Cursor cursor;
    BancoController crud;
    String codigo;


    public EditPvpFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_pvp, container, false);

        codigo = getActivity().getIntent().getStringExtra("codigo");

        crud = new BancoController(getActivity().getBaseContext());

        assunto = (TextView)view.findViewById(R.id.editTextAssunto);
        comoEstou = (EditText)view.findViewById(R.id.editComoEstou);
        comoDeveria = (EditText)view.findViewById(R.id.editComoDeveriaEstar);
        oQueFazer = (EditText)view.findViewById(R.id.editoQueVouFazer);
        comoFazer = (EditText)view.findViewById(R.id.editComoVouFazer);
        quandoFazer = (TextView)view.findViewById(R.id.editTxtDate);


        salvarEdicao = (Button)view.findViewById(R.id.editBtnSalvar);

        cursor = crud.carregaDados(Integer.parseInt(codigo));

        assunto.setText(cursor.getString(cursor.getColumnIndexOrThrow(CriaBanco.ASSUNTO)));
        comoEstou.setText(cursor.getString(cursor.getColumnIndexOrThrow(CriaBanco.COMOESTOU)));
        comoDeveria.setText(cursor.getString(cursor.getColumnIndexOrThrow(CriaBanco.COMODEVERIA)));
        oQueFazer.setText(cursor.getString(cursor.getColumnIndexOrThrow(CriaBanco.OQUEFAZER)));
        comoFazer.setText(cursor.getString(cursor.getColumnIndexOrThrow(CriaBanco.COMOFAZER)));
        quandoFazer.setText(cursor.getString(cursor.getColumnIndexOrThrow(CriaBanco.ATEQUANDO)));


        salvarEdicao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                crud.alteraRegistro(Integer.parseInt(codigo),
                        assunto.getText().toString(),comoEstou.getText().toString(),
                        comoDeveria.getText().toString(),oQueFazer.getText().toString(),
                        comoFazer.getText().toString(),quandoFazer.getText().toString());

// Tentei realizar algo aqui, mas das coisas que tentei realizar não deu certo
                String recebe = getArguments().getString("codigo");


                /*Intent intent = new Intent(getActivity(),ShowPvpFragment.class);
                startActivity(intent);*/

            }
        });
        return view;
    }
}

What needs to be done?

1 answer

0


On the line

codigo = getActivity().getIntent().getStringExtra("codigo");

change to

codigo = getArguments().getString("codigo");
  • Thank you very much! I gave it right here :) I will only have to check when the Fragment is empty otherwise it will give dick, but what I wanted to do was right. Thank you! God bless :)

Browser other questions tagged

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