【问题标题】:Android customer adpter scrolling issueAndroid自定义适配器滚动问题
【发布时间】:2013-10-21 06:23:00
【问题描述】:

我将在 textview 中显示具有固定标题 invno 日期等的详细信息。而下面是与客户 adpter 一起显示的列表视图,并在最后一行显示客户 adpter 中的总颜色不同@

但是当我滚动两三次时会导致这样的问题

这是我的客户适配器

public class CustomAdapter extends BaseAdapter {
private Activity activity;
    private LayoutInflater inflater=null;
    private RelativeLayout relativeCel;
    private ArrayList<String> listidA=new ArrayList<String>();
    private ArrayList<String> listDateA=new ArrayList<String>();
    private ArrayList<String> lisTotalAMTA=new ArrayList<String>();
    private ArrayList<String> listPaymenIdA=new ArrayList<String>();
    private ArrayList<String> listPaymenNameA=new ArrayList<String>();
    private Float total=0.0f;


    /**
     * 
     * @param a
     * @param listid
     * @param listDate
     * @param lisTotalAMT
     * @param listPaymenId
     * @param listPaymenName
     */
    public CustomAdapter(Activity a,ArrayList<String>listid,ArrayList<String>listDate,ArrayList<String>lisTotalAMT,ArrayList<String>listPaymenId,ArrayList<String>listPaymenName) {
        inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.activity=a;
        this.listidA=listid;
        this.listDateA=listDate;
        this.lisTotalAMTA=lisTotalAMT;
        this.listPaymenIdA=listPaymenId;
        this.listPaymenNameA=listPaymenName;
        total=0.0f;

        for(int i=0;i<listidA.size();i++)
            total=total+(Float.parseFloat(""+lisTotalAMTA.get(i)));

    }
    public int getCount() {
        return listidA.size()+1;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public class ViewHolder{
        TextView txtInvoiceNo,txtDate,txtPaymentMode ,txtPamentAmount;
    }
    @SuppressLint("NewApi")
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            AppLog.logString(TAG+"if Null: "+position);
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.item_sales_report, null);
            holder = new ViewHolder();
            holder.txtInvoiceNo = (TextView)convertView.findViewById(R.id.txtInvoiceNo);
            holder.txtDate = (TextView)convertView.findViewById(R.id.txtDate);
            holder.txtPaymentMode = (TextView)convertView.findViewById(R.id.txtPaymentMode); 
            holder.txtPamentAmount = (TextView)convertView.findViewById(R.id.txtPamentAmount);

            convertView.setTag(holder);
        } else {
            AppLog.logString(TAG+"else Null: "+position);
            holder = (ViewHolder) convertView.getTag();
        }

        if(position-1==listPaymenIdA.size()-1){
            //AppLog.logString(TAG+"if: "+position);
            int color=getResources().getColor(R.color._90ffffff);
            holder.txtInvoiceNo.setBackgroundColor(color);
            holder.txtDate.setBackgroundColor(color);
            holder.txtPamentAmount.setBackgroundColor(color);
            holder.txtPaymentMode.setBackgroundColor(color);

            holder.txtInvoiceNo.setTypeface(null, Typeface.BOLD);
            holder.txtDate.setTypeface(null, Typeface.BOLD);
            holder.txtPamentAmount.setTypeface(null, Typeface.BOLD);
            holder.txtPaymentMode.setTypeface(null, Typeface.BOLD);

            holder.txtInvoiceNo.setText("");
            holder.txtDate.setText("Total ");
            holder.txtPamentAmount.setText(""+round(total));
            holder.txtPaymentMode.setText("");
        }else{
            //AppLog.logString(TAG+"else: "+position);
            Date date1 = null;
            try {
                date1 = new SimpleDateFormat("yyyy/MM/dd").parse(listDateA.get(position));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            formatterYY = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
            String todaye = formatterYY.format(date1);
            holder.txtInvoiceNo.setText(""+listidA.get(position));
            holder.txtDate.setText(""+todaye);
            holder.txtPaymentMode.setText(""+listPaymenNameA.get(position));
            holder.txtPamentAmount.setText(""+round(lisTotalAMTA.get(position)));
        }
        return convertView;
    }
}

【问题讨论】:

    标签: android android-listview android-custom-view


    【解决方案1】:

    将此代码放入您的列表视图中,

    android:cacheColorHint = "#000000". 
    

    【讨论】:

    • 为了什么?我已经放了。阅读我的问题和图像,我必须在底部显示总计,但在客户适配器中,我有 textview 的 chageg 颜色,当滚动时会影响不同的单元格
    • 你可以让它使用 ArrayAdapter 代替吗?我对此也很陌生,甚至看不到如何使用 BaseAdapter 类。问题在于 if(position-1==listPaymenIdA.size()-1){。它不只是为最后一项返回 true。所以。正在为其他项目设置文本效果,当它们被重新使用时,它会采用不会重置这些属性的其他路径。
    【解决方案2】:

    只是改变

    if(position-1==listPaymenIdA.size()-1){
            //AppLog.logString(TAG+"if: "+position);
            int color=getResources().getColor(R.color._90ffffff);
            holder.txtInvoiceNo.setBackgroundColor(color);
            holder.txtDate.setBackgroundColor(color);
            holder.txtPamentAmount.setBackgroundColor(color);
            holder.txtPaymentMode.setBackgroundColor(color);
    
            holder.txtInvoiceNo.setTypeface(null, Typeface.BOLD);
            holder.txtDate.setTypeface(null, Typeface.BOLD);
            holder.txtPamentAmount.setTypeface(null, Typeface.BOLD);
            holder.txtPaymentMode.setTypeface(null, Typeface.BOLD);
    
            holder.txtInvoiceNo.setText("");
            holder.txtDate.setText("Total ");
            holder.txtPamentAmount.setText(""+round(total));
            holder.txtPaymentMode.setText("");
        }else{
            //AppLog.logString(TAG+"else: "+position);
            Date date1 = null;
            try {
                date1 = new SimpleDateFormat("yyyy/MM/dd").parse(listDateA.get(position));
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            int color=getResources().getColor(R.color._10ffffff);//normal color which display
            holder.txtInvoiceNo.setBackgroundColor(color);
            holder.txtDate.setBackgroundColor(color);
            holder.txtPamentAmount.setBackgroundColor(color);
            holder.txtPaymentMode.setBackgroundColor(color);
    
            holder.txtInvoiceNo.setTypeface(null, Typeface.NORMAL);
            holder.txtDate.setTypeface(null, Typeface.NORMAL);
            holder.txtPamentAmount.setTypeface(null, Typeface.NORMAL);
            holder.txtPaymentMode.setTypeface(null, Typeface.NORMAL);
    
            formatterYY = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
            String todaye = formatterYY.format(date1);
            holder.txtInvoiceNo.setText(""+listidA.get(position));
            holder.txtDate.setText(""+todaye);
            holder.txtPaymentMode.setText(""+listPaymenNameA.get(position));
            holder.txtPamentAmount.setText(""+round(lisTotalAMTA.get(position)));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多