I have got a problem with my BaseAdapter. I am using a Gridview to show my content. And i need to add some blankfield. But if i scroll up and down the blankfields move randomly.
My Question is: How can i make them stay at the right position?
public class GridviewAdapter extends BaseAdapter {
private ArrayList<String> sign;
private ArrayList<String> roman;
private ArrayList<String> check;
private Activity activity;
private int minusValue=0;
private boolean fulllist;
public GridviewAdapter(Activity activity, ArrayList<String> sign,ArrayList<String> roman,ArrayList<String> check,boolean fulllist) {
super();
this.activity=activity;
this.sign=sign;
this.roman=roman;
this.check=check;
this.fulllist=fulllist;
}
@Override
public int getCount() {
if(fulllist)
{
return sign.size()+5;
}
else
{
return sign.size();
}
}
public void updateList(ArrayList<String> newcheck) {
check.clear();
check.addAll(newcheck);
this.notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
return sign.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder
{
public TextView signv;
public TextView romanv;
public TextView checkv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view= new ViewHolder();
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridblock, null);
view.signv = (TextView) convertView.findViewById(R.id.sign);
view.checkv = (TextView) convertView.findViewById(R.id.check);
view.romanv = (TextView) convertView.findViewById(R.id.roman);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
if(position==36) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
minusValue=minusValue-1;
}
else if(position==38) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
minusValue=minusValue-1;
}
else if(position==46) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
minusValue=minusValue-1;
}
else if(position==47) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
minusValue=minusValue-1;
}
else if(position==48) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
minusValue=minusValue-1;
}
else
{
view.signv.setText(sign.get(0));
view.checkv.setText(check.get(0));
if( view.checkv.getText().equals("✘"))
{
view.checkv.setTextColor(Color.RED);
}
else
{
view.checkv.setTextColor(Color.GREEN);
}
view.romanv.setText(roman.get(0));
}
return convertView;
}
In the else clause, when updating all the ViewHolder
fields, you need to undo the invisibility applied for the blank positions. Eg:
convertView.setVisibility(View.VISIBLE);
convertView.setClickable(true);
convertView.setEnabled(true);
This is because recycled views are not guaranteed to be the same position as when they were initially inflated. Meaning the view used for position 47 may be reused for position 2.
I'm not sure what you are doing with the minusValue
but you may need to adjust that as well. Keep in mind, getView()
can and will be called 3-4 times per position number. Just an FYI when using that variable.