Android:滚动时ListView搞砸了

时间:2015-02-14 07:42:15

标签: android database listview

我正在使用listview显示抽奖结果,结果存储在dababase Records中,编码如下:

代码:

生成列表:

   public void generate_listing()
    {
        String action = "recordId " + "DESC";
        Record = Select.from(Records.class).orderBy(action).list();

        recordId_list = new ArrayList<Integer>();
        result_list = new ArrayList<String>();

        recordId_list.clear();
        result_list.clear();
        lv1.invalidateViews();
        for (int i = 0; i < Record.size(); ++i) 
        {
            recordId_list.add(Record.get(i).getrecordId());
            result_list.add(Record.get(i).getResult());
        }
        lv1.setAdapter(new My_LV_ArrayAdapter(RecordListActivity2.this)); 
    } 

适配器:

    private class My_LV_ArrayAdapter extends BaseAdapter 
    {
        private LayoutInflater mInflater; 
        private Context mContext = null;  
        public My_LV_ArrayAdapter(Context context) 
        {  
            mContext = context;  
            mInflater = LayoutInflater.from(mContext);  
        }  

        public int getCount() 
        {
            return recordId_list.size();
        }

        public Object getItem(int arg0) 
        {
            return recordId_list.get(arg0);
        }

        public long getItemId(int position) 
        {
            return position;
        }   

        @Override
        public int getViewTypeCount() 
        {

            if (getCount() != 0)
                return getCount();
            return 1;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {
            if (convertView == null) 
            {
                convertView = mInflater.inflate(R.layout.list_item2, null);         
            }
            String xx = Record.get(position).getResult();
            String [] temp = null;  
            temp = xx.split("-");     
            ArrayList<Integer> tempArray = new ArrayList<Integer>();
            for (int k=0; k<6; k++)
            {
                tempArray.add(Integer.valueOf(temp[k]));
            }
            final LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.ll);           

            // inflate slot number
            String yy = period_list.get(position);
            ll.addView(convertTextToImageView(width, height, (position+1)+". "+temp2[1]));

            // inflate slot results
            for (int p = 1; p <= 10; p++)
            {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w,w);
                btn_ball = new Button(mContext);                
                btn_ball.setEnabled(false);
                if (tempArray.contains(p))
                {
                    btn_ball.setBackgroundResource(R.drawable.btn_green_selector);
                }
                else
                {
                    btn_ball.setBackgroundResource(R.drawable.btn_transparent_selector);
                }

                ll.addView(btn_ball, params);
            }   
            return convertView;
        }
    } 

编辑:

public View getView(int position, View convertView, ViewGroup parent) 
    {
        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.list_item3, null);         
        }
        String xx = Record.get(position).getResult();
        String [] temp = null;  
        temp = xx.split("-");     
        ArrayList<Integer> tempArray = new ArrayList<Integer>();
        for (int k=0; k<6; k++)
        {
            tempArray.add(Integer.valueOf(temp[k]));
        }

        DisplayMetrics localDisplayMetrics = getResources().getDisplayMetrics();
        Constants.SCREEN_W = localDisplayMetrics.widthPixels;
        Constants.SCREEN_H = localDisplayMetrics.heightPixels;

        int hh = Constants.SCREEN_W/51;
        int ww = Constants.SCREEN_W/51;
        ImageButton btn_slot = (ImageButton) convertView.findViewById(R.id.btn_slot);

        // inflate slot number
        String yy = period_list.get(position);
        String [] temp2 = null;  
        temp2 = yy.split("-");
        btn_slot.setImageBitmap(convertTextToBM(Constants.SCREEN_W/25, ww, (position+1)+". "+temp2[1]));
        List<ImageButton> list_btn  = new ArrayList<ImageButton>();
        for(int id : BUTTON_IDS) 
        {
            ImageButton imgBtn = (ImageButton)convertView.findViewById(id);
            imgBtn.setEnabled(false);
            imgBtn.getLayoutParams().height = hh;
            imgBtn.getLayoutParams().width = ww;
            list_btn.add(imgBtn);
        }

        // inflate slot results
        for (int p = 0; p < 49; p++)
        {
            if (tempArray.contains(p))
            {
                list_btn.get(p).setBackgroundResource(R.drawable.btn_green_selector);
list_btn.get(p).setImageResource(BUTTON_IMG[p]);
            } 
            else
            {
                list_btn.get(p).setBackgroundResource(R.drawable.btn_transparent_selector);
list_btn.get(p).setImageResource(BUTTON_IMG[p]);
            }               
        }   
        return convertView;
    }

问题:

最初以降序排列的结果。但是,在列表视图上下滚动时,列表已更改,不再排序。有些项目甚至重复。有些人走了。

发生了什么事?谢谢!

编辑:

我已将list_item3修改为要扩展到列表视图的布局,其中内部连续有50个ImageButtons。适配器中的getView部分如上所述进行了修改。但是,在向上或向下滚动列表视图后,夸大的结果仍然是混乱的。怎么能解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:1)

您正在添加到回收的视图。对于convertview不为null的情况,它将是您已调用addView因此重复的旧布局之一。

理想情况下,您不应该在getView中添加视图,但如果您真的想这样做,则可以检查非null转换视图是否已经具有您要使用ID或标记添加的视图。

我建议您构建布局,这样您只需在列表视图中设置内容(数据/侦听器等),而不是在getView

中实际添加内容

查看this以更好地了解列表视图的工作原理。