简体   繁体   中英

Same data displaying in Listview using custom adapterview from the sqlite

i am having error in getting user details in 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..

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. you need to create new object of Searchmodel each time in for loop .

Remove searchmodel global variable .

I assume name of class is Searchmodel then you can try this:

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

      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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