francesco bocci
francesco bocci

Reputation: 69

Why when i refresh my list it duplicates?

this is my problem: First i have a listview like this:

image1

When I click on first row (for expemple prod3) appears my dialog like this (this is for delete clicked row):

image2

But when i refresh my fragment happens this thing:

image3

Why? there is a function like "revalidate, repaint" (java) on Android ?

This is my code when i click on row:

  public void mostraProdotto(String tito){

    final  EditText textprod;
    final  EditText prezzo;

    Button btnConf;
    Button btnDelete;
    final Dialog dialogCustom = new Dialog(getActivity());

    dialogBuilder  = new AlertDialog.Builder(getActivity());
    //process

    dialogCustom.setContentView(R.layout.spesa_pagata);
    dialogCustom.setTitle("Nome Prod");
    textprod = (EditText)dialogCustom.findViewById(R.id.textprod);
    textprod.setText(tito);
    prezzo = (EditText)dialogCustom.findViewById(R.id.txtprezzo);
    btnConf = (Button)dialogCustom.findViewById(R.id.btnConf);


    btnConf.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            String nomeprod = textprod.getText().toString();
            String strprezzo = prezzo.getText().toString();

            if(!nomeprod.isEmpty() && !strprezzo.isEmpty()) {

       ..............


               new getSpesa().execute(); //getSpesa is for see listview
                dialogCustom.cancel();



        }

        }
    });

and this is code to fill list:

                    listView = (ListView) rootView.findViewById(R.id.list_spesa);
                    listView.setY(20);
                    adapter = new CustomListAdapterSpesa(getActivity(), movieList);
                    adapter.notifyDataSetChanged();
                    listView.setAdapter(adapter);

EDIT:

this is my customadapter

public class CustomListAdapterSpesa extends BaseAdapter {

private Activity activity;

private LayoutInflater inflater;
private List<MovieSpesa> movieItems;

int pos;
public CustomListAdapterSpesa(Activity activity, List<MovieSpesa> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
}

@Override
public int getCount() {
    return movieItems.size();
}

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);


    TextView title = (TextView) convertView.findViewById(R.id.title);

      TextView genre = (TextView) convertView.findViewById(R.id.genre);
  convertView.findViewById(R.id.releaseYear);

    MovieSpesa m = movieItems.get(position);


    int coint = getCount();


    // title
    title.setText(m.getTitle());



    // genre


    genre.setText(m.getGenre());

    return convertView;

}

}

Upvotes: 0

Views: 52

Answers (1)

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

The Adapter is written correctly, so the problem might be on list side.

You posted this code:

listView = (ListView) rootView.findViewById(R.id.list_spesa);
listView.setY(20);
adapter = new CustomListAdapterSpesa(getActivity(), movieList);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);

The problem must be on how movieList is used, it must has a class-scope and not a local scope.

So you have two possible solutions:

  1. Use movieList.clear(); before adding the other items. This method clears all items previouvsly added. In this way the only items inside the list will be the newly inserted ones.
  2. You can Simply istantiate the list before the usage. (this means locally) In this way every time you call the method filling the list, the object itself will be re-created.

Hope this helps.

Upvotes: 1

Related Questions