1

我有一个库存检查应用程序,其中材料显示在网格中,每个网格元素都有一个材料图像、数量的编辑文本、检查按钮和一个根据库存是否可用而变成红色或绿色的图像。还有一个文本视图,如果图像为绿色,则设置为“可用”,如果图像为红色,则设置为“不可用”。

我正在使用扩展基本适配器的自定义适配器。

一切正常,直到我按下检查按钮

我使用edittext textwatcher来更改文本在哪个位置但是当我按下复选按钮时,图像在3 * 3网格的第9个元素处设置为绿色。

我正在使用 viewHolder 但它仍然无法正常工作。

任何人都可以请帮忙。

请看下面的代码

private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
    private Context context;
    boolean[] checkboxstate;
    ViewHolder viewHolder;
    ArrayList<HashMap<String, Object>> testdata;

    public CustomAdapter(Context context, int textViewResourceId,
            ArrayList<HashMap<String, Object>> testdata) {
        super(context, textViewResourceId, testdata);
        this.context=context;
        this.testdata=testdata;
        checkboxstate = new boolean[testdata.size()];
    }

    private class ViewHolder {

        TextView txt_product;
        ImageView image_available;
        ImageView image_product;
        TextView txt_available;
        EditText quantity;
        Button check;

    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        View gridView;

        if (convertView == null) {

            gridView=new View(context);
            convertView = inflater.inflate(R.layout.salesdummy, null);
            viewHolder = new ViewHolder();

            // cache the views

            viewHolder.quantity=(EditText)convertView.findViewById(R.id.quantity);
            viewHolder.image_product=(ImageView)convertView.findViewById(R.id.image1);
            viewHolder.image_available=(ImageView)convertView.findViewById(R.id.imagecheck);
            viewHolder.txt_available=(TextView)convertView.findViewById(R.id.txt_available);
            viewHolder.check=(Button)convertView.findViewById(R.id.btncheck);
            viewHolder.txt_product=(TextView)convertView.findViewById(R.id.product);



            // link the cached views to the convertview
            convertView.setTag(viewHolder);

        } else
            viewHolder = (ViewHolder) convertView.getTag();

        // set the data to be displayed


        viewHolder.txt_product.setText(testdata.get(position).get("MATERIAL").toString());

        int photoId=(Integer) testdata.get(position).get("IMAGE");
        viewHolder.image_product.setImageDrawable(getResources().getDrawable(photoId));

    /*  viewHolder.quantity.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                quant=s.toString();
            }
        });
        */
        viewHolder.check.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            Log.d("VAlue of string",quant);
            int i_quant;
            i_quant=Integer.parseInt(quant);

            String material=testdata.get(position).get("MATERIAL").toString();
            Log.d("Check material",material);

            Cursor c=db.GetMaterial(material);
            rec_quantity=c.getString(2);

            System.out.println("Value of received quantity is" + rec_quantity);
            i_rec_quantity=Integer.parseInt(rec_quantity);

            if(i_quant>i_rec_quantity)
            {
                System.out.println("===============Value is greater than stock");

                viewHolder.image_available.setBackgroundDrawable(getResources().getDrawable(R.drawable.red));
                viewHolder.txt_available.setText("Not Available");

            }
            else
            {
                viewHolder.image_available.setBackgroundDrawable(getResources().getDrawable(R.drawable.green));
                viewHolder.txt_available.setText("Available");
            }

            }
        });

        viewHolder.quantity.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                quant=s.toString();
            }
        });

        /*
        viewHolder.txt_name.setText(testdata.get(position)
                .get("NAME").toString());

        viewHolder.txt_salesvolume.setText(testdata.get(position)
                .get("SALESVOLUME").toString());
        viewHolder.txt_overdue.setText(testdata.get(position)
                .get("OVERDUE").toString());
        viewHolder.txt_outstanding.setText(testdata.get(position)
                .get("OUTSTANDING").toString());
        viewHolder.txt_creditlimit.setText(testdata.get(position)
                .get("CREDIT_LIMIT").toString());
        viewHolder.txt_creditutilization.setText(testdata.get(position)
                .get("CREDIT_UTILIZATION").toString());

        String creditlimit=testdata.get(position).get("CREDIT_LIMIT").toString();
        String creditutilized=testdata.get(position).get("CREDIT_UTILIZATION").toString();
*/



        /*
        String remcom=testdata.get(position).get("CREDIT_UTILIZATION").toString();
        Log.d("Remcom value",remcom);
        String remcom1=remcom.replace(",","");
        int inint=Integer.parseInt(remcom1);

        if(inint>150000)
        {
            viewHolder.txt_creditutilization.setTextColor(Color.RED);
        }

        */

        // VITAL PART!!! Set the state of the
        // CheckBox using the boolean array

        // //HAVE OPENED THE BELOW COMMENT AND HAVE DONE THIS TO CHECK
        // WHETHER DUPLICATE CHECKS ARE NOT IMPLEMENTED


        // return the view to be displayed
        return convertView;
    }

}
4

0 回答 0