我想在列表视图中添加两个功能:
1)在长按上,我要删除该行。
2)一旦行是
删除我想更改文件编号,以便它总是在
订购。
例如:-我有一个doc_no IN1000,IN1001,IN1002和我的列表
使用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);
}
这是列表的我的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;
}
}
我是编程新手,因此非常感谢您提供任何帮助或建议。谢谢。
最佳答案
我认为在您的情况下使用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();
}