0

我有一个列表视图,它使用一个特殊的适配器将数据从数据库传递到列表视图。但问题是我只得到一个列表视图项目被重复,而其他细节没有被显示。当我设置断点并调试项目时,所有细节都被传递给哈希图,但列表视图只重复显示一个特定项目。代码如下所示:

static class ViewHolder {
    TextView txtmername,txtmerid,txtmeradd,txtmermeasure;
    Button btnselect;
}



    private LayoutInflater mInflater;

           public SpecialAdapter(Context ctx,List<HashMap<String, String>> listData, int resourceId, String[] columnTags, int[] columnIds) {
                super(ctx, listData, resourceId, columnTags, columnIds);
                ctx=MerchantList.this;
                listData=listData;
                resourceId=R.layout.merchant_listview;
                columnTags=columnTags;
                columnIds=columnIds;
            }

           @Override
        public int getCount() {
        return listData.size();
        }

           @Override
        public Object getItem(int position) {
        return super.getItem(position);
        }

    @Override
    public long getItemId(int position) {
    return super.getItemId(position);
    } 

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.   
        ViewHolder holder;
        Context con=getApplicationContext();

        if(convertView==null)
            // convertView = getActivity().getLayoutInflater().inflate(R.layout.stores_listview_layout, pa
             mInflater = (LayoutInflater)con.getSystemService(con.LAYOUT_INFLATER_SERVICE);
            convertView=mInflater.inflate(R.layout.merchantlistview, null);

             holder = new ViewHolder();
                    holder.txtmername = (TextView) convertView.findViewById(R.id.lblMerchantName);
                    holder.txtmeradd=(TextView)convertView.findViewById(R.id.lblAddress);
                    holder.txtmerid=(TextView)convertView.findViewById(R.id.lblMerchantId);
                   holder.txtmermeasure=(TextView)convertView.findViewById(R.id.lblMeasure);
                   holder.btnselect=(Button)convertView.findViewById(R.id.btnSelect);


                   holder.btnselect.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        });

                    convertView.setTag(holder);
         if(convertView !=null)
        {
            holder=(ViewHolder)convertView.getTag();


        // Binding the data efficiently with the holder

        SQLiteDatabase db=openOrCreateDatabase("NEW.db", MODE_PRIVATE, null);
        Cursor OrderCursor = db.rawQuery("SELECT DISTINCT(A.ID), B.NAME, B.ADDRESS, A.MEASURE FROM (SELECT ID, MIN( CAST(STOCK_IN_HAND AS REAL) /REORDER_LEVEL) AS MEASURE FROM INVENTORY GROUP BY ID) A INNER JOIN MASTER B ON A.ID=B.ID ORDER BY A.MEASURE", null);

        listData.clear();

        if(OrderCursor!= null) 
        {

         if(OrderCursor.moveToFirst()){
               for (int i = 0; i < OrderCursor.getCount(); i++){


                // String first,second,third,fourth=null;

                        HashMap<String,String> map = new HashMap<String, String>();

                        map.put(columnTags[0], OrderCursor.getString(OrderCursor.getColumnIndex("NAME")));   
                        map.put(columnTags[1], OrderCursor.getString(OrderCursor.getColumnIndex("ADDRESS")));
                        map.put(columnTags[2], OrderCursor.getString(OrderCursor.getColumnIndex("ID")));
                        map.put(columnTags[3], OrderCursor.getString(OrderCursor.getColumnIndex("MEASURE")));
                        listData.add(map);

                           String measure = map.get("measure").toString();
                           String name=map.get("Name").toString();
                           String address=map.get("Address").toString();
                           String id=map.get("Id").toString();

                        holder.txtmerchantname.setText(name);
                        holder.txtmeradd.setText(address);
                        holder.txtmerid.setText(id);
                        holder.txtmermeasure.setText(measure);
                       double measure1=Double.parseDouble(measure);

                       if(measure1 > 1.5)
                       {
                           convertView.setBackgroundColor(getResources().getColor(R.color.Green));
                       }

                       else if((1.5 >= measure1 ) && (measure1>1.0))
                       {

                          convertView.setBackgroundColor(getResources().getColor(R.color.Yellow));
                       }

                       else if(1.0>=measure1) 
                       {
                           convertView.setBackgroundColor(Color.RED);
                       }

                 OrderCursor.moveToNext();

                 }//end of for
          }
        OrderCursor.close();
         db.close();
    }
        }
        return convertView;
    }
    @Override
    public int getItemViewType(int position) {
    return super.getItemViewType(position);
    }
    @Override
    public int getViewTypeCount() {
    return super.getViewTypeCount();
    }
}
4

2 回答 2

0

尝试使用以下代码,我想这会有所帮助。因此,您正在使用可以使用 arrayAdapter 的 hashmap。

https://stackoverflow.com/a/15061779/2106338

于 2013-02-26T12:08:12.740 回答
0

getViewTypeCount()与您的listView项目数量不匹配,因此它会添加另一个项目。
在您的情况下,我认为它与measure您使用的条件有关。
最后你应该使用else而不是else if
OR
如果有其他情况,你必须治疗它。

于 2015-04-16T12:08:38.533 回答