java - 如何在android中操纵 Volley 响应?

标签 java android listview android-volley

我想在我的 ListView 中添加两个功能:-

1)On Long click I want to delete the row.

2)And once the row is deleted I want to change the document numbers so that it is always in order.

For eg:- I have a list with doc_no IN1000,IN1001,IN1002 and I delete the row with doc_no IN1001. What I would like to do is change the doc_no of IN1002 to IN1001.So that it is always in a sequence.

到目前为止,我已经成功地使用 parent.removeViewInLayout(view) 删除了一行;但是如果我滚动 ListView 就会出现问题,我会找回已删除的行。

这是我删除行的代码:-

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);
}

这是我的列表适配器类的代码:-

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;
    }
}

我是编程新手,所以非常感谢任何帮助或建议。谢谢。

最佳答案

我认为使用 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();

            }

关于java - 如何在android中操纵 Volley 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37788954/

相关文章:

java - 不直接编写 Servlet 来创建 REST API 的原因

java - 我如何检查是否有东西离开屏幕? - 框架

java - Android 上 Java 中的数据库事务

android - 如何在互联网可用时存储和发送队列文件

android - ListView:如何从外部以编程方式访问 Item 的元素?

c# - 如何在 C# 中使用 Caliburn.Micro 更新 ListView 中的项目?

java - @OneToOne 或 @ManyToOne 引用未知实体

java - 数据刷新后滚动条移动

java - 方法 getExternalStorageDirectory() 已弃用

c# - 具有 AutoScroll 性能的虚拟化 ListView(慢)