vendredi 4 mars 2016

listview showing duplicate values on execution

When I try to load data from SQLite database to my list view each item is duplicated.I double checked my database values while getting to the curser. there is no redundant data in it.but when I try to show in listview i get duplicate data.i tried other Questions from this section.but nothing can solve it.I know that hash set can reduce redundant data.But I have no idea about how to use it.please help.thanks in advance. here is my code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.distance);

     list = (ListView) findViewById(R.id.list);
     openAndQueryDatabase();
     displayResultList();

}

private void displayResultList() {

    String[] fromwhere = { "code","name" };
    int[] viewswhere = { R.id.id,R.id.name};
          ADAhere = new SimpleAdapter(Distance.this, datas,
            R.layout.simple_list_row, fromwhere, viewswhere);
    list.setAdapter(ADAhere);
  list.setOnItemClickListener(this);

    }


    private void openAndQueryDatabase() {
    try {

       datas = new ArrayList<Map<String, String>>();
    DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
    newDB = dbHelper.getWritableDatabase();
    Cursor c = newDB.rawQuery("select distinct cust_code,cust_name from customer", null);

    Log.v("detailss", c.toString());

        while (c.moveToNext()){
        HashMap<String, String> datanums = new HashMap<String, String>();
        String custcode = c.getString(c.getColumnIndex("cust_code"));
        datanums.put("code", custcode);
        datas.add(datanums);

        String name = c.getString(c.getColumnIndex("cust_name"));
        datanums.put("name", name);

         datas.add(datanums);

        }
    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
       } 

    }

Aucun commentaire:

Enregistrer un commentaire