0

I'm facing a strange behaviour using an ArrayAdapter.

When the number of listview item exceed the height of the listView (say after item 8), the next item get the id 0 instead the id 9.

In my opinion this type of issue was explained here with the convertView, but i use it in the same way (i think).

The following code is my ArrayAdapter.

 public class StepsAdapter extends ArrayAdapter<String> {


    Context context;
    List<String> steps;

    public StepsAdapter(Context context, int resourceId, List<String> steps) {
        super(context, resourceId, steps);
        this.context = context;
    }

    private class ViewHolder {
        EditText stepValue;
        ImageView removeStep;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final String step = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row_step, null);
            holder = new ViewHolder();
            holder.stepValue = (EditText) convertView.findViewById(R.id.stepEdit);
            holder.removeStep = (ImageView) convertView.findViewById(R.id.removeStep);
            holder.stepValue.setText(step);

            holder.removeStep.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"* Remove id step " + position, Toast.LENGTH_LONG).show();
                    steps.remove(position);
                    notifyDataSetChanged();
                }
            });

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }
 }

Then my main activity where i get existing data and put it in my listView, the add button and the save button.

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_game);

            mContext = getApplicationContext();

            steps = new ArrayList<String>();
            stepsAdapter = new StepsAdapter(mContext,R.layout.row_step,steps);

            Gson gson = new GsonBuilder().create();
            game = gson.fromJson(gameJson, Games.class);

            /*
             * Settings values
             */

            gameNameValue.setText(game.getName());
            gameBackgroundPreview.setBackgroundColor(game.getColor());
            colorSelected = game.getColor();

            for(int i = 0; i < game.getSteps().size() ; i++){
                //steps.add(game.getSteps().get(i).toString());
                //notifyDataSetChanged();
                stepsAdapter.add(game.getSteps().get(i).toString());
            }

            final ListView listSteps = (ListView) findViewById(R.id.listViewSteps);

            listSteps.setAdapter(stepsAdapter);
            gameNameValue.setText(gameName);


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

                    stepsId = steps.size();
                    Toast.makeText(getApplicationContext(), "addSteps : " + stepsId, Toast.LENGTH_LONG).show();
                    stepsAdapter.insert("newstep", stepsId);
                }
            });

            buttonSaveGame.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String valueEditGameName = gameNameValue.getText().toString();
                    int valueColorBackaground = colorSelected;
                    String picture = "testPic";

                    for(int i=0; i < listSteps.getChildCount(); i++) {
                        LinearLayout rowLayout = (LinearLayout) listSteps.getChildAt(i);
                        //Log.e(TAG, ">> :) layout  >>" + listSteps.getChildAt(i).getClass().getName());
                        EditText editRow = (EditText) rowLayout.getChildAt(0);
                        stepsValues.add(editRow.getText().toString());
                        //Log.e(TAG, ">> :) inside layout >>" + editRow.getText().toString());
                    }

                    if(valueEditGameName.trim().length() > 0 && picture.trim().length() >0 ){

                        Games game = new Games(valueEditGameName,valueColorBackaground,picture,stepsValues);
                        String goToSave = game.createJson();
                        Log.e(TAG, ">>Saved>>" + goToSave);
                        final CkdFile file = new CkdFile();
                        String saved  = file.writeToSDFile(game.getName(), goToSave);

                        Toast.makeText(mContext, saved, Toast.LENGTH_LONG).show();
                        Intent backToMain = new Intent(mContext,MainActivity.class);
                        startActivity(backToMain);

                    } else {
                        Toast.makeText(mContext, "Fill all texts", Toast.LENGTH_LONG).show();
                    }

                }
            });
         }

I try to add items in 2 different ways :

  • add item through : List steps
  • add item through : StepsAdapter stepsAdapter

Both give me same behaviour.

If someone has a clue to help understanding what i'm doing wrong with my implementation of ListView/ArrayAdapter.

Thanks in advance !

EDIT 1 : After pushing some logs everywere, it understand the strange behaviour : My adapter have only 6 slots (the limit came from the size of the listview in layout), and when my arraylist have more than 6 items, the getView select items only between 0 and 5.

I'm searching now a way to get the position in ArrayList and not the position in arrayadapter.

Community
  • 1
  • 1
mako
  • 83
  • 8

2 Answers2

0

I faced same issue recently. Add following overrides to Adapter:

@Override
public int getViewTypeCount() {
    return getCount();
}

@Override
public int getItemViewType(int position) {
    return position;
}
Karan
  • 2,120
  • 15
  • 27
  • It happens with convertview, it recycles the first row to n+1 row and above two function resolve it by defining each position to convertview – Karan May 30 '15 at 14:48
  • It seems to be the same, i will add some logs and check to see my state of list and adapter. – mako May 30 '15 at 15:38
0

I found a simple xml "trick" to avoid this behaviour : i set a biger height to listView.

<ListView
    android:layout_width="match_parent"
    android:layout_height="1000dp"
    android:layout_gravity="center_horizontal"
    android:id="@+id/listViewSteps"
    android:layout_margin="10dp">
</ListView>

It's not really resolve but a take it ...

mako
  • 83
  • 8