简体   繁体   English

使用来自 sqlite 的自定义适配器视图在 Listview 中显示相同的数据

[英]Same data displaying in Listview using custom adapterview from the sqlite

i am having error in getting user details in sqlite.我在 sqlite 中获取用户详细信息时出错。

i want to display the these three user details by searching the data base but it showing the same user details three times not three different user details.我想通过搜索数据库来显示这三个用户详细信息,但它显示相同的用户详细信息三次而不是三个不同的用户详细信息。

Here is my code这是我的代码

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);

i am retriving the details in the AsynchTask and in doingBackGround is in which i am having problem with loop please any body solve it..我正在retriving在细节AsynchTaskdoingBackGround是在我有循环问题,请任何机构解决这个问题..

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);




                }

                    }



                }

my customer adapter class is我的客户适配器类是

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;
    }

}

You are using searchmodel variable globally.您正在全局使用searchmodel variable you need to create new object of Searchmodel each time in for loop .您需要每次在for loop创建Searchmodel新对象。

Remove searchmodel global variable .删除searchmodel global variable

I assume name of class is Searchmodel then you can try this:我假设类的名称是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM