Listview with Arrayadapter

Asked

Viewed 57 times

0

Hello guys I’m new in Android and I’m with a question of data overwriting in listview that I haven’t been able to find yet. I have a Tabbed with 3 tabs, and when clicking the first time tab the listview appears correctly but when switching tab and returning to Tab from listview it duplicates the data!

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);


}  

}

//In the image below I changed tab and went back to tool where duplicated the tools inserir a descrição da imagem aqui

1 answer

0

I believe I have found the problem: Previously I was using in my Tabs "Sectionspagerarpter" and kind of custom. I reworked them using "Fragmentstatepageradapter" and the problem is gone. I can’t tell if the problem itself was Adapter or something from the tabs.

BEFORE: ....

    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);

AFTERWARD: ...

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;
}

Browser other questions tagged

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