Arraylist <hashmap <>> values ​​repeated in listview

I am trying to get the contents of a Sqlite database in listview using a simple adapter. I use arraylist> to get all the values. It appears in the list, but the values ​​seem to be repeated every time I run it. what should I do? Here is 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);
           }
+3
source share
3 answers

Just declare an ArrayList before the onCreate method, for example ...

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

And initialize this ArrayList when you want to put the extracted values ​​into an ArrayList, for example ...

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

, ( ). getView(), , ...

,

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

&

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

You checked your db. Does it have duplicate values ​​???

If not, try adding values ​​to Arraylist only if they do not already exist in the list.

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

EDIT

Since you are facing a problem when you run it. Verify that the database is not populating rows with values OR

just re-initialize your arraylists.eg myArrList = new ArrayList<String>();

0
source

All Articles