如何使用以前添加的值填充列表视图编辑文本?



在我的main activity中,我使用dialog在单击按钮时显示listview,该listview包含三个textview,一个edittext,一个完成button。通过单击完成button,我只显示那些包含edittextusing 循环中的值的行在我的main activitylinear layout,现在我想要的是当我再次重新打开列表dialog时,edittext字段将包含以前添加的那些值。我该怎么做?

这是在dialog中填充listview的代码,

allProducts = dh.gettAllProductInfo();
adapter = new ProductListAdapter(getApplicationContext(), allProductss);
productListView.setAdapter(adapter);

这是点击button完成的时候...

doneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<productinfo> AddedItems = new ArrayList<productinfo>();
try
{
for(int i = 0;i<allProductss.size();i++)
{   
View view = productListView.getChildAt(i);
EditText qty = (EditText) view.findViewById(R.id.quantity_edittext);
String qtyVal = qty.getText().toString();
TextView product = (TextView) view.findViewById(R.id.product_textview);
String pName = product.getText().toString();
TextView p_code = (TextView) view.findViewById(R.id.product_code);
String pCode = p_code.getText().toString();
if(qty.getText().toString().matches(""))
{
//Do nothing
}
else
{
AddedItems.add(new productinfo(pCode, pName, "", qtyVal, ""));
}
}

我尝试根据我对您想要的内容进行说明。此外,您需要使其适应您的代码工作。这是一个例子:

private ArrayList<productinfo> AddedItems;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you have to put this part of code when you show the dialog so that 
// it will be executed all the time you open the dialog 
if(AddedItems != null){
allProducts.addAll(AddedItems);
AddedItems = null;
}
else{
allProducts = dh.gettAllProductInfo();
}
adapter = new ProductListAdapter(getApplicationContext(), allProductss);
productListView.setAdapter(adapter);
doneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AddedItems = new ArrayList<productinfo>();          
try
{
for(int i = 0;i<allProductss.size();i++)
{   
View view = productListView.getChildAt(i);
EditText qty = (EditText) view.findViewById(R.id.quantity_edittext);
String qtyVal = qty.getText().toString();
TextView product = (TextView) view.findViewById(R.id.product_textview);
String pName = product.getText().toString();
TextView p_code = (TextView) view.findViewById(R.id.product_code);
String pCode = p_code.getText().toString();
if(!qty.getText().toString().matches(""))
{
AddedItems.add(new productinfo(pCode, pName, "", qtyVal, ""));
}
}
}   
catch(Exception e){}
}
}
}

最新更新