使用来自 sqlite 的自定义适配器视图在 Listview 中显示相同的数据
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);
我正在检索 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);
}
}
}
我的客户适配器 class 是
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
.
我假设 class 的名字是 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);
}
}
}
我在 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);
我正在检索 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);
}
}
}
我的客户适配器 class 是
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
.
我假设 class 的名字是 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);
}
}
}