【问题标题】:Arraylist<hashmap<>> values repeating in listviewArraylist<hashmap<>> 值在列表视图中重复
【发布时间】:2014-03-07 08:59:50
【问题描述】:

我正在尝试使用简单的适配器将 Sqlite 数据库的内容放入列表视图。 我正在使用 arraylist> 来获取所有值。它显示在列表视图上,但每次运行它时这些值似乎都会重复。 我该怎么办? 这是我的代码

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

【问题讨论】:

    标签: android listview arraylist hashmap


    【解决方案1】:

    只需在 onCreate 方法之前声明 ArrayList 就像...

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

    当您想将检索到的值放入 ArrayList 时,请初始化此 ArrayList,例如...

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

    【讨论】:

    • 如何从哈希图中删除相同的值?
    【解决方案2】:

    我认为,问题出在您的列表适配器(简单适配器)中。请检查您的列表适配器的 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 );
    

    【讨论】:

      【解决方案3】:

      你检查过你的数据库。它有重复的值吗???

      如果没有 - 仅当列表中不存在值时尝试在 Arraylist 中添加值

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

      编辑

      因为您在重新运行时遇到了问题。请确保数据库不会使用保存值重新填充行

      只需重新初始化您的数组列表。例如myArrList = new ArrayList&lt;String&gt;();

      【讨论】:

      • 数据库没有重复值
      猜你喜欢
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2013-05-30
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多