juco3 juco3 - 3 months ago 8
Android Question

Showing List<object> in listView

Sorry for my bad english.this is part of a data base that show in a listview names of clients of a store.I have the following code:

private void updateCustomerList(){
mdatabase=new MiDataBase(mcontext);
customerList=new ArrayList<Customer>();
customerList=mdatabase.getallCustomer();//this call getallCustumer
customerListStr=new ArrayList<String>();
mlistView=getListView();
if(customerList==null||customerList.size()<=0){
customerListStr.add("NO SE HAN A√ĎADIDO CLIENTES");
unregisterForContextMenu(mlistView);
}else{
Customer item=new Customer();
for(int i=0;i<customerList.size();i++){
item=customerList.get(i);
customerListStr.add(item.getName());
}
registerForContextMenu(mlistView);
}
MyArrayAdapter marrayAdapter=new MyArrayAdapter(mcontext,customerListStr);
mlistView.setAdapter(marrayAdapter);
}


Method getallCustomer is:

public List<Customer>getallCustomer(){

ArrayList<String>marrayList=new ArrayList<String>();
List<Customer> mcustomerList=new ArrayList<Customer>();
marrayList.add(Customer.camposCust.name.toString());
Customer mcustumer=new Customer();
String[]marrayColumn=marrayList.toArray(new String[marrayList.size()]);
Cursor mcursor;
openDB();
mcursor=database.query(nombreT,marrayColumn,null,null,null,null,null);
mcursor.moveToFirst();
int mindex=mcursor.getColumnIndex(Customer.camposCust.name.toString());
while(mcursor.isAfterLast()==false){
mcustumer.setName(mcursor.getString(mindex));
mcustomerList.add(mcustumer);
mcursor.moveToNext();
}
mcursor.close();
closeDB();
return mcustomerList;
}


and the Customer class is:

public class Customer
{
private String city;
private String address;
private String cel;
private String email;
private String name;
private int customerID;

public enum camposCust{
city,
address,
cel,
email,
name,
_customerID
}
public void setCity(String city)
{
this.city = city;
}

public String getCity()
{
return city;
}

public void setAddress(String address)
{
this.address = address;
}

public String getAddress()
{
return address;
}

public void setCel(String cel)
{
this.cel = cel;
}

public String getCel()
{
return cel;
}

public void setEmail(String email)
{
this.email = email;
}
public String getEmail()
{
return email;
}

public void setName(String name)
{
this.name = name;
}

public String getName()
{
return name;
}
public void setCustomerID(int customerID)
{
this.customerID = customerID;
}

public int getCustomerID()
{
return customerID;
}
}


the problem is when I introduce two or more items, in the listview just show the last item introduced more than once,like the following picture:for example I introduced three items:Juan, Peter and Joseph. Juan and Peter doesn't show

enter image description here

I hope you understand and someone can helpe please. I will appreciate, thanks in advance.

Answer
ArrayList<String>marrayList=new ArrayList<String>();
    List<Customer> mcustomerList=new ArrayList<Customer>();
    marrayList.add(Customer.camposCust.name.toString());
    // Customer mcustumer=new Customer();
    String[]marrayColumn=marrayList.toArray(new String[marrayList.size()]);
    Cursor mcursor;
    openDB();
    mcursor=database.query(nombreT,marrayColumn,null,null,null,null,null);
    mcursor.moveToFirst();
    int mindex=mcursor.getColumnIndex(Customer.camposCust.name.toString());
    while(mcursor.isAfterLast()==false){
     Customer mcustumer=new Customer();
        mcustumer.setName(mcursor.getString(mindex));
        mcustomerList.add(mcustumer);
        mcursor.moveToNext();
    }
    mcursor.close();
    closeDB();
    return mcustomerList;
}



create the customer inside loop.