0

I have got a listview which uses a special adapter to pass data into the listview from the database.But the problem is that I am only getting one listview item being repeated and the other details are not being displayed. When I put breakpoints and debug the project, all the details are being passed to the hashmap, but the listview is only showing one particular item repeatedly. The code is shown below:

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();
    }
}
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Jei
  • 11
  • Fetch you data somewhere else, `getView();` is used for binding data to `View`s. For starters, I suggest you move you data fetching code to your constructor and use `getView();` for binding purpose only. – M-Wajeeh Feb 26 '13 at 12:23
  • Possible duplicate of [Custom ListView Items repeated](http://stackoverflow.com/questions/13985240/custom-listview-items-repeated) – M D P Nov 01 '16 at 06:15

2 Answers2

0

Try using the following code which i guess will be helpful. Hence you are using the hashmap the arrayAdapter can be used.

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

Community
  • 1
  • 1
Droidee
  • 94
  • 5
0

getViewTypeCount() is not matched with your listView items count therefore it adds up another item.
In your case I think its about the measure condition you are using.
In the end you should use else instead of else if
OR
if there is another condition, you must treat it.

Salmaan
  • 3,543
  • 8
  • 33
  • 59