Search code examples
androidandroid-cursoradapter

Why Android CursorAdapter call 3 times the same action


I have a problem with my CursorAdapter, I have to fill a listView with dynamic images.

I have a BBDD with some fields with value "1" or "0".

In bindView, when the fields returns 1 I add the image to a linearLayout.

The problem is that the image is added 3 times, and I don't know why.

girlsFragmentAdapter:

public class girlsListFragmentCursoAdapter extends CursorAdapter {



public girlsListFragmentCursoAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.girls_list_row,parent,false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView girlName;
    ImageView imgExercice = new ImageView(context);
    LinearLayout linearImgRow = (LinearLayout) view.findViewById(R.id.exercices_list_row);

    girlName = view.findViewById(R.id.txtvwGirlName);
    if (cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")).equals("1") ) {
        imgExercice.setImageResource(R.drawable.push_ups);
        linearImgRow.addView(imgExercice);
        Toast.makeText(context,"Pull ups: " +cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")),Toast.LENGTH_SHORT ).show();
    }
    girlName.setText(cursor.getString(cursor.getColumnIndexOrThrow("nombre")));





}

}

I call this adapter from a Fragment ** when **OnActivityCreated is called

girls_fragment

public class girls_fragment extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";




private String mParam1;
private String mParam2;
private WodDbAdapter mWodAdapter;
ListView lvWods;
private  View rootview;



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

public static girls_fragment newInstance(String param1, String param2) {
    girls_fragment fragment = new girls_fragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    lvWods = (ListView) getView().findViewById(R.id.lvwGirlsList);
    mWodAdapter = new WodDbAdapter(getContext());
    mWodAdapter.open();
    fillData();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootview = inflater.inflate(R.layout.fragment_girls_fragment,container,false);

    return rootview;
}

private void fillData(){
    Cursor mGirlsCursor = mWodAdapter.fetchAllWods("girl");
    getActivity().startManagingCursor(mGirlsCursor);
    String [] from = new String[]{WodDbAdapter.KEY_NOMBRE,};
    int [] to = new int []{R.id.txtvwGirlName};

    girlsListFragmentCursoAdapter mGirlsAdapter = new girlsListFragmentCursoAdapter(rootview.getContext(),mGirlsCursor);

    lvWods.setAdapter(mGirlsAdapter);
}

}

The result is this:

enter image description here

As you can see the image appears three times instead of one


Solution

  • It's because the following code:

    if (cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")).equals("1") ) {
        imgExercice.setImageResource(R.drawable.push_ups);
        linearImgRow.addView(imgExercice);
        Toast.makeText(context,"Pull ups: " +cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")),Toast.LENGTH_SHORT ).show();
    }
    

    with the following line:

        linearImgRow.addView(imgExercice);
    

    where you're always adding the imgExercice to the linearImgRow each times the bindView is called.