我想在listview中添加两个功能: -
1)长按一下,我想删除该行。
2)一旦排了 删除我想更改文档编号,以便它始终在 顺序。
例如: - 我有一个doc_no IN1000,IN1001,IN1002和I的列表 删除doc_no IN1001的行。我想做的是改变 IN1002到IN1001的doc_no。它总是在一个序列中。
到目前为止,我已成功使用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);
}
这是我的适配器类的代码: -
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;
}
}
我是编程新手所以非常感谢任何帮助或建议。谢谢。
答案 0 :(得分:1)
在您的适配器中创建名为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();
}
});
答案 1 :(得分:1)
我认为在您的情况下使用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();
}