[英]Same data displaying in Listview using custom adapterview from the sqlite
我在 sqlite 中获取用户详细信息时出错。
我想通过搜索数据库来显示这三个用户详细信息,但它显示相同的用户详细信息三次而不是三个不同的用户详细信息。
这是我的代码
Cursor c= mydatabase.query(true, TABLENAME_1, new String[] { KEY_Name,
KEY_CellPhoneNumber,KEY_CompanyName,KEY_Email_id }, KEY_Name + " LIKE ?",
new String[] {"%"+ name+ "%" }, null, null, null,
null);
我正在retriving在细节AsynchTask
和doingBackGround
是在我有循环问题,请任何机构解决这个问题..
DataHelper databasehelper = new DataHelper(Search.this);
databasehelper.open();
Cursor c= databasehelper.getAllCustomerDetailsByName(_name);
if(c!=null && c.getCount()>0) {
int nam = c.getColumnIndex(KEY_Name);
int cellnumber = c.getColumnIndex(KEY_CellPhoneNumber);
int company = c.getColumnIndex(KEY_CompanyName);
int emailid = c.getColumnIndex(KEY_Email_id);
flag_data = true;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
searchmodel.setName(c.getString(nam));
searchmodel.setCellphone(c.getString(cellnumber));
searchmodel.setCompany(c.getString(company));
searchmodel.setEmailid(c.getString(emailid));
Log.i("Search", searchmodel.getName());
Log.i("Search", searchmodel.getCellphone());
Log.i("Search", searchmodel.getCompany());
Log.i("Search", searchmodel.getEmailid());
for (int i = 0; i < c.getCount();i++) {
customerSearch.add(i,searchmodel);
Log.i("index", ""+i);
}
}
}
我的客户适配器类是
public class CustomerSearchAdapter extends BaseAdapter {
private ArrayList<CustomerSearchModel> customerSearchData;
private LayoutInflater layoutInflater;
private Context _context;
public CustomerSearchAdapter(Context context,
ArrayList<CustomerSearchModel> listData) {
this.customerSearchData = listData;
_context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return customerSearchData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return customerSearchData.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (layoutInflater == null)
layoutInflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_user_details,
null);
holder = new ViewHolder();
holder.customer_user_name = (TextView) convertView
.findViewById(R.id.user_name);
holder.customer_company_name = (TextView) convertView
.findViewById(R.id.company_name);
holder.customer_phone_number = (TextView) convertView
.findViewById(R.id.phone_number);
holder.customer_emailid_number = (TextView) convertView
.findViewById(R.id.emailid_number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final CustomerSearchModel customDetailItem = (CustomerSearchModel) customerSearchData
.get(position);
holder.customer_user_name.setText(customDetailItem.getName());
holder.customer_company_name.setText(customDetailItem.getCompany());
holder.customer_phone_number.setText(customDetailItem.getCellphone());
holder.customer_emailid_number.setText(customDetailItem.getEmailid());
return convertView;
}
static class ViewHolder {
TextView customer_user_name;
TextView customer_company_name;
TextView customer_phone_number;
TextView customer_emailid_number;
}
}
您正在全局使用searchmodel
variable
。 您需要每次在for loop
创建Searchmodel
新对象。
删除searchmodel
global variable
。
我假设类的名称是Searchmodel
然后你可以试试这个:
DataHelper databasehelper = new DataHelper(Search.this);
databasehelper.open();
Cursor c= databasehelper.getAllCustomerDetailsByName(_name);
if(c!=null && c.getCount()>0) {
int nam = c.getColumnIndex(KEY_Name);
int cellnumber = c.getColumnIndex(KEY_CellPhoneNumber);
int company = c.getColumnIndex(KEY_CompanyName);
int emailid = c.getColumnIndex(KEY_Email_id);
flag_data = true;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
Searchmodel searchmodel = new Searchmodel();
searchmodel.setName(c.getString(nam));
searchmodel.setCellphone(c.getString(cellnumber));
searchmodel.setCompany(c.getString(company));
searchmodel.setEmailid(c.getString(emailid));
Log.i("Search", searchmodel.getName());
Log.i("Search", searchmodel.getCellphone());
Log.i("Search", searchmodel.getCompany());
Log.i("Search", searchmodel.getEmailid());
for (int i = 0; i < c.getCount();i++) {
customerSearch.add(i,searchmodel);
Log.i("index", ""+i);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.