0

Olá pessoal sou novo em Android e estou com uma dúvida de sobrescrita de dados no listview que ainda não consegui encontrar. Tenho uma Tabbed com 3 tabs, e ao clicar a primeira vez na tab aparece o listview corretamente porém ao trocar de tab e retornar para Tab do listview ele duplica os dado!

public class TabAgendaFerramentas extends Fragment{

public static Context context;

//Início código ref. ListView com checkbox
public ListView listViewFerramentas;
public ArrayList<String> arrayListFerramentas=new ArrayList<>();
//Fim código ref. ListView com checkbox

String campoSituacaoAgenda = "ABERTA";


View view;
public TabAgendaFerramentas(){}

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


    //Inicio método chama menu tabFerramentas
    setHasOptionsMenu(true);
    //Fim método chama menu tabFerramentas

    //Chamar listar ferramentas
    listarFerramentas();
    //Fim chamar listar ferramentas

    return view;
} 

public void listarFerramentas(){
    listViewFerramentas = (ListView) view.findViewById(R.id.listview);
    listViewFerramentas.setAdapter(null);
    List<ModeloFerramentas> modeloferramentas = new FerramentaController(getContext()).listarFerramenta();
    for (ModeloFerramentas ferramenta : modeloferramentas) {
        int id = ferramenta.getIdFerramenta();
        String nomeFerramenta = ferramenta.getNomeFerramenta();
        float valorFerramenta = ferramenta.getValorFerramenta();

        arrayListFerramentas.add("CÓD: " + id + " FERRAMENTA: " + nomeFerramenta + "\n" + "R$: " + valorFerramenta);
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, arrayListFerramentas);
    listViewFerramentas.setAdapter(adapter);


}  

}

//Na imagem abaixo eu mudei de tab e voltei para ferramenta onde duplicou as ferramentas inserir a descrição da imagem aqui

1 Resposta 1

0

Acredito que eu tenha encontrado o problema: Anteriormente eu estava utilizando em minhas Tabs "SectionsPagerAdapter" e meio que personalizadas. Reformulei elas utilizando "FragmentStatePagerAdapter" e o problema sumiu. Não sei dizer se o problema em si era o adapter ou algo das tabs.

ANTES: ....

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.containeragendamento);
    mViewPager.setAdapter(mSectionsPagerAdapter);



    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.containeragendamento);
    mViewPager.setAdapter(mSectionsPagerAdapter);

DEPOIS: ...

public class PagerAdapter extends FragmentStatePagerAdapter {

//integer to count number of tabs
int tabCount;

//Constructor to the class
public PagerAdapter(FragmentManager fm, int tabCount) {
    super(fm);
    //Initializing tab count
    this.tabCount= tabCount;
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .