在Custom ListView中,ArrayAdapter在滚动时给出错误的位置

时间:2017-06-18 12:47:01

标签: java android listview

我从数据库中获取13项并尝试使用ArrayAdepter将其设置为自定义列表视图,但问题是每当我滚动到底部时,位置顺序为0,1,2,3,4,5,6,7 ,8,9,10,11,12,但如果我从下往上滚动它给出错误的位置,如6,5,4,3,2,1,0,12,11,......

总是从底部滚动中获得错误的位置。

我已经阅读并实现了stackoverflow上的所有解决方案,但没有解决它。

这是我的arrayadepter代码

public class ListAdapter_home extends ArrayAdapter {

List list = new ArrayList();

Context contect;
SharedPreferences savep;
SharedPreferences.Editor editor;


public ListAdapter_home(Context context, int resource) {
    super(context, resource);
    contect = context;
}

static class DataHandler {
    CustomTextView name;



}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

@Override
public void remove(Object object) {
    super.remove(object);
    list.remove(object);



}

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

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    View row;
    row = convertView;
    savep = getContext().getSharedPreferences("MySave", getContext().MODE_PRIVATE);
    editor = savep.edit();



    final DataHandler handler;

    if (convertView == null) {

        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.home_card, parent, false);
        handler = new DataHandler();

        handler.name = (CustomTextView) row.findViewById(R.id.catname);


        row.setTag(handler);

    } else {


        handler = (DataHandler) row.getTag();

    }

    Log.d("position",""+position);
    final HomeList dataProvider;
    dataProvider = (HomeList) this.getItem(position);

   handler.name.setText(dataProvider.getName());



    return row;
}}

0 个答案:

没有答案