滚动android时ListView项显示错误?

时间:2015-06-24 16:35:58

标签: android listview android-listview

我试图在ListView中显示一些单词。但问题是当我开始滚动时ListView没有填充下一个单词。它会随机填充相同的重复单词。

DictListAdapter:

public class DictListViewAdapter extends BaseSwipeAdapter {

    private Context mContext;
    private ArrayList<String> searchResult;
    private String word;
    private LayoutInflater inflater;
    TextView wrd;

    public DictListViewAdapter(Context mContext, ArrayList<String>searchResult) {
        this.mContext = mContext;
        this.searchResult = searchResult;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public View generateView(final int position, ViewGroup parent,View v) {
           if(inflater==null){
               inflater = (LayoutInflater) mContext
                       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           }
           if(v==null) {
               v = inflater.inflate(R.layout.listview_item,null);
           }


        wrd = (TextView) v.findViewById(R.id.word);
        final TextView meaning=(TextView)v.findViewById(R.id.meaning);

        SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
               // YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
                //Toast.makeText(mContext, DatabaseHelper.getSingleMeaning(searchResult.get(position), mContext), Toast.LENGTH_SHORT).show();
                String mean=DatabaseHelper.getSingleMeaning(searchResult.get(position),mContext);
                meaning.setText(mean);
            }
        });
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {
                Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
            }
        });
        /*v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "click delete", Toast.LENGTH_SHORT).show();
            }
        });
        */

        word = searchResult.get(position);
        wrd.setText(word);


        return v;
    }

这个类扩展了BaseSwipeAdapter,并且它具有如下所示的getView()方法,它具有getView()的实现:

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if(v == null){
            v = generateView(position, null,convertView);
        }
        mItemManger.bind(v, position);
        fillValues(position, v);
        return v;
    }

我知道“每次ListView需要在屏幕上显示一个新行时,它会从其适配器调用getView()方法。”这就是为什么我添加了 if(v = = null)但是没有工作。但是我已经在我的其他项目中完成了这个调整。它工作正常但是在这个项目中它不起作用。任何解决方案?提前谢谢。

编辑:我尝试过使用ViewHolder但没有工作。我想念什么吗?

static class ViewHolder {
        TextView wrd;
        int position;
    }

    public DictListViewAdapter(Context mContext, ArrayList<String>searchResult) {
        this.mContext = mContext;
        this.searchResult = searchResult;
    }




    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public View generateView(final int position, ViewGroup parent,View v) {
          ViewHolder viewHolder;
           if(inflater==null){
               inflater = (LayoutInflater) mContext
                       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           }
           if(v==null) {

               v = inflater.inflate(R.layout.listview_item,null);
               viewHolder = new ViewHolder();
               viewHolder.wrd=(TextView) v.findViewById(R.id.word);
               viewHolder.position=position;
               v.setTag(viewHolder);
           }else{
               viewHolder=(ViewHolder)v.getTag();
           }


        viewHolder.wrd.setText(searchResult.get(position));
        final TextView meaning=(TextView)v.findViewById(R.id.meaning);

        SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
               // YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
                //Toast.makeText(mContext, DatabaseHelper.getSingleMeaning(searchResult.get(position), mContext), Toast.LENGTH_SHORT).show();
                String mean=DatabaseHelper.getSingleMeaning(searchResult.get(position),mContext);
                meaning.setText(mean);
            }
        });
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {
                Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
            }
        });
        /*v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "click delete", Toast.LENGTH_SHORT).show();
            }
        });
        */

        return v;
    }

1 个答案:

答案 0 :(得分:0)

如果你必须使用ViewHolder模式并检查generateView方法中的空视图,那么你将不得不更改BaseSwipeAdapter中的代码

@Override
public final View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    // if(v == null){

    v = generateView(position, v, parent);
    // }
    mItemManger.bind(v, position);
    fillValues(position, v);
    return v;
}

评论此空检查并立即尝试。