简体   繁体   中英

Arraylist<hashmap<>> values repeating in listview

I am trying to get the contents of a Sqlite database into a listview using simple adapter. I am using a arraylist> to get all the values. It is getting displayed on the listview but the values seem to repeat everytime i run it. what should i do? Here's my code

             ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>();
              Databasehandler db=new Databasehandler(this);

              private ListView lv;
                  protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
             setContentView(R.layout.contacts);
             val=db.getAllContacts();
                   final ListView lv=(ListView)findViewById(R.id.listView1);


               if(val.size()!=0)
                {
             ListAdapter ad=new SimpleAdapter(Contact.this,val,R.layout.v,new String[]{"contacts"},new int[]{R.id.textView2});

               lv.setAdapter(ad);
           }

Just declare the ArrayList before onCreate method like...

ArrayList<HashMap<String, String>>val = null;

And initialize this ArrayList when you want put retrieved values into the ArrayList like...

val=new ArrayList<HashMap<String,String>>();

I think, problem is in your list adapter(Simple Adapter). Please check your list adapter's getView() is add twise of view, just check out your adapter with following...

Just use this custom adapter,

public class CustomAdapter extends BaseAdapter {

    private List<SearchObjects> mObjects = null;
    private LayoutInflater mInflater = null;

    public CustomAdapter (Context context, List<SearchObjects> mData) {
        mObjects = mData;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return mObjects.size();
    }

    public Object getItem(int position) {
        return mObjects.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
         if (convertView == null) 
         {
              convertView = mInflater.inflate(R.layout.search_items_single_row,null);
              holder = new ViewHolder();
              holder.txtvName = (TextView) convertView.findViewById(R.id.txtvItemName);
              convertView.setTag(holder);
         } 
         else
             holder = (ViewHolder) convertView.getTag();
         holder.txtvName.setText(mObjects.get(position).getDL_TRADENAME());
         return convertView;
     }

     private class ViewHolder {
         TextView txtvName = null;
     }
}

& call it as,

mCustomListAdapter = new RxNewSearchDrugsListAdapter(getActivity(), mSearchObjects);
mListView.setAdapter(mCustomListAdapter );

Did you check your db .Does it have duplicate values???

If not - try adding values in Arraylist only if they do not exist already in the list

for(each..str){
if(!myArrList.contains(str)){
myArrList.add(str);
}

EDIT

Since you face the prob when you re run it .Make sure the database does not repopulate rows with save values OR

just reinitialize your arraylists .eg myArrList = new ArrayList<String>();

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