繁体   English   中英

如何在Android中操纵凌空反应?

[英]How to manipulate volley response in android?

我想在列表视图中添加两个功能:

1)在长按上,我要删除该行。

2)一旦删除该行,我想更改文档编号,以使其始终保持秩序。

例如:-我有一个带有doc_no IN1000,IN1001,IN1002的列表,然后删除带有doc_no IN1001的行。 我想做的是将IN1002的doc_no更改为IN1001,以便它始终按顺序排列。

到目前为止,我已经能够使用parent.removeViewInLayout(view);成功删除一行。 但是如果我滚动列表视图,则会出现问题,返回已删除的行。

这是我删除行的代码:

lv_bsall.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(final AdapterView<?> parent, final View view, int position, long id)
    {
        final int pos = position;
        final Dialog delete_expense = new Dialog(ReportGenerator.this);
        delete_expense.setContentView(R.layout.delete_payment);
        delete_expense.setTitle("DO YOUY WANT TO DELETE Invoice");
        Button yes = (Button) delete_expense.findViewById(R.id.yes);
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                parent.removeViewInLayout(view);
                doc_no = ArrayUtils.removeElement(doc_no,doc_no[pos]);
                balance =ArrayUtils.removeElement(balance,balance[pos]);
                total =ArrayUtils.removeElement(total,total[pos]);
                vat =ArrayUtils.removeElement(vat,vat[pos]);
                profit=ArrayUtils.removeElement(profit,profit[pos]);

                delete_expense.dismiss();

            }
        });

        Button no = (Button) delete_expense.findViewById(R.id.no);
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                delete_expense.dismiss();
            }
        });

        delete_expense.show();



        return true;
    }
});

这是我要求响应的方法:-

public void showBS(String response) {
    ParseBS_all pb = new ParseBS_all(response);
    pb.parseBS();

    doc_no =ParseBS_all.doc_no;
    balance =ParseBS_all.balance;
    total =ParseBS_all.total;
    vat=ParseBS_all.vat;
    profit=ParseBS_all.profit;

    bl = new BS_allList(this, doc_no, balance, total, vat, profit);
    lv_bsall.setAdapter(bl);
}

这是列表的我的Adapter类的代码:

public class BS_allList extends ArrayAdapter<String>
{

    private String[] doc_no;
    private String[] balance;
    private String[] total;
    private String[] vat;
    private String[] profit;
    private Activity context;

    public BS_allList(Activity context, String[] doc_no, String[]balance, String[] total, String[] vat, String[] profit)
    {
        super(context, R.layout.bs_list_all, doc_no);
        this.context =context;
        this.doc_no= doc_no;
        this.balance = balance;
        this.total = total;
        this.vat=vat;
        this.profit = profit;
    }



    @Override
    public View getView(int position, View listViewItem, ViewGroup parent)
    {
        if (null == listViewItem)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            listViewItem = inflater.inflate(R.layout.bs_list_all, null, true);
        }
        TextView tv_docNo = (TextView) listViewItem.findViewById(R.id.tvdoc_no);
        TextView tv_balance = (TextView) listViewItem.findViewById(R.id.tv_balance);
        TextView tv_tot = (TextView) listViewItem.findViewById(R.id.tv_total);
        TextView tv_vat = (TextView) listViewItem.findViewById(R.id.tv_vat);
        TextView tv_pf = (TextView) listViewItem.findViewById(R.id.tv_profit);

        tv_docNo.setText(doc_no[position]);
        tv_balance.setText(balance[position]);
        tv_tot.setText(total[position]);
        tv_vat.setText(vat[position]);
        tv_pf.setText(profit[position]);


        return listViewItem;
    }
}

我是编程新手,因此非常感谢您提供任何帮助或建议。谢谢。

在适配器中创建一个名为deleteRow的方法并将position作为参数传递。 像这样:

public void deleteRow(int position)
{
    doc_no = ArrayUtils.removeElement(doc_no, doc_no[position]);
    total = ArrayUtils.removeElement(total, total[position]);
    balance = ArrayUtils.removeElement(balance, balance[position]);
    vat = ArrayUtils.removeElement(vat, vat[position]);
    profit = ArrayUtils.removeElement(profit, profit[position]);
    notifyDataSetChanged();
}

在您的LongClick中调用它:

 yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {                
           // Here 'bl' is the object of your 'BS_allList' adpater

            bl.deleteRow(position);
            parent.removeViewInLayout(view);
            delete_expense.dismiss();

        }
    });

我认为在您的情况下使用ArrayList应该会有所帮助。 请尝试此解决方案。它可以满足您的两个要求:

  public void onClick(View v)
            {
                ls_docno = new ArrayList<String>(Arrays.asList(doc_no));
                ls_balance = new ArrayList<String>(Arrays.asList(balance));
                ls_total =new ArrayList<String>(Arrays.asList(total));
                ls_vat= new ArrayList<String>(Arrays.asList(vat));
                ls_profit =new ArrayList<String>(Arrays.asList(profit));

                ls_docno.remove(pos);
                ls_balance.remove(pos);
                ls_total.remove(pos);
                ls_profit.remove(pos);
                ls_vat.remove(pos);

                Log.d("POSITION",String.valueOf(pos));

                for (int i=pos; i< ls_docno.size(); i++)
                {
                    if(i>0)
                    {
                        String doc= ls_docno.get(i-1);
                        String inv_no = doc.replaceAll("[^0-9]", "");
                        int new_invno = Integer.parseInt(inv_no);
                        new_invno++;
                        ls_docno.set(i,"IN"+new_invno);

                    }

                }

                doc_no = ls_docno.toArray(new String[ls_docno.size()]);
                balance = ls_balance.toArray(new String[ls_balance.size()]);
                total = ls_total.toArray(new String[ls_total.size()]);
                profit = ls_profit.toArray(new String[ls_profit.size()]);
                vat = ls_profit.toArray(new String[ls_vat.size()]);

                bl = new BS_allList(ReportGenerator.this, doc_no, balance, total, vat, profit);
                lv_bsall.setAdapter(bl);


                delete_expense.dismiss();

            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM