While loop only shows the last record in android studio

Viewed 33

I created an Arraylist in my db_operation.java

public ArrayList<House> viewAllHouse(){
    SQLiteDatabase db = getReadableDatabase();
    String sql = "SELECT * FROM HOUSE";
    Cursor cursor = db.rawQuery(sql,null);
    ArrayList<House> houses = new ArrayList<>();
    if (cursor.getCount()>0){
        while (cursor.moveToNext()){
            House house=new House();
            house.setHid(cursor.getInt(0));
            house.setOwner(cursor.getString(1));
            house.setAddress(cursor.getString(2));
            house.setContact(cursor.getInt(3));
            house.setRooms(cursor.getInt(4));
            house.setBathrooms(cursor.getInt(5));
            house.setFlooring(cursor.getString(6));
            house.setPrice(cursor.getInt(7));
            house.setImg(cursor.getBlob(8));
            houses.add(house);
            Log.i("gggggggggggggggggggggggggggggggg", String.valueOf(houses.get(0)));
        }

    }else{
        houses=null;
    }
    return houses;
}

and followed by the HouseAdapter.java

public class HouseAdapter extends BaseAdapter {
    TextView HouseID, HouseOwner, HouseAddress, HouseContact, HouseRooms, HouseBathrooms, HouseFlooring, HousePrice;
    ImageView HouseImage;


    Context context;
    ArrayList<House> houses;

    public HouseAdapter(Context context, ArrayList<House> houses) {
        this.context = context;
        this.houses = houses;
    }

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

    @Override
    public Object getItem(int i) {
        return houses.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view1 = inflater.inflate(R.layout.custom_house_view,viewGroup,false);

        HouseID=view1.findViewById(R.id.lblhouseID);
        HouseOwner=view1.findViewById(R.id.lblhouseOwner);
        HouseAddress=view1.findViewById(R.id.lblhouseAddress);
        HouseContact=view1.findViewById(R.id.lblhouseContact);
        HouseRooms=view1.findViewById(R.id.lblhouseRooms);
        HouseBathrooms=view1.findViewById(R.id.lblhouseBathrooms);
        HouseFlooring=view1.findViewById(R.id.lblhouseFlooring);
        HousePrice=view1.findViewById(R.id.lblhousePrice);
        HouseImage=view1.findViewById(R.id.lblhouseImg);


        House house= houses.get(i);
        HouseID.setText("ID: "+house.getHid());
        HouseOwner.setText("Owner: "+house.getOwner());
        HouseAddress.setText("Address: "+house.getAddress());
        HouseContact.setText("Contact: "+house.getContact());
        HouseRooms.setText("Rooms: "+house.getRooms());
        HouseBathrooms.setText("Bathrooms: "+house.getBathrooms());
        HouseFlooring.setText("Floring: "+house.getFlooring());
        HousePrice.setText("Price: "+house.getPrice());

        Bitmap bitmap= BitmapFactory.decodeByteArray(house.getImg(),0,house.getImg().length);
        HouseImage.setImageBitmap(bitmap);
        return view1;
    }
}

and this is where I show the customer the houselist

public class Cleaner_Interface extends AppCompatActivity {

    ArrayList<House> houses;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cleaner_interface);
        DB_Operation db = new DB_Operation(this);
        ListView HouseList = findViewById(R.id.lsthouseList);
        houses= db.viewAllHouse();
        try {
            HouseAdapter adapter= new HouseAdapter(this,houses);
            HouseList.setAdapter((ListAdapter) adapter);
        }catch (Exception e){
            Toast.makeText(Cleaner_Interface.this, "NO Products Available", Toast.LENGTH_SHORT).show();
        }

    }

}

but the issue is it only shows the last record only. if i have 3 records it shows 3 records but the last record is repeated 3 times. please help pardon my lack of coding knowledge

this is the issue

1 Answers

You have declare all TextView as global so it's override and showing only last record.

Please declare inside getView method

public class HouseAdapter extends BaseAdapter {

    Context context;
    ArrayList<House> houses;

    public HouseAdapter (Context context, ArrayList<House> houses) {
        this.context = context;
        this.houses = houses;
    }

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

    @Override
    public Object getItem(int i) {
        return houses.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater =(LayoutInflater) context . getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        View view1 = inflater . inflate (R.layout.custom_house_view, viewGroup, false);

        TextView HouseID, HouseOwner, HouseAddress, HouseContact, HouseRooms, HouseBathrooms, HouseFlooring, HousePrice;
        ImageView HouseImage;

        HouseID = view1.findViewById(R.id.lblhouseID);
        HouseOwner = view1.findViewById(R.id.lblhouseOwner);
        HouseAddress = view1.findViewById(R.id.lblhouseAddress);
        HouseContact = view1.findViewById(R.id.lblhouseContact);
        HouseRooms = view1.findViewById(R.id.lblhouseRooms);
        HouseBathrooms = view1.findViewById(R.id.lblhouseBathrooms);
        HouseFlooring = view1.findViewById(R.id.lblhouseFlooring);
        HousePrice = view1.findViewById(R.id.lblhousePrice);
        HouseImage = view1.findViewById(R.id.lblhouseImg);


        House house = houses . get (i);
        HouseID.setText("ID: " + house.getHid());
        HouseOwner.setText("Owner: " + house.getOwner());
        HouseAddress.setText("Address: " + house.getAddress());
        HouseContact.setText("Contact: " + house.getContact());
        HouseRooms.setText("Rooms: " + house.getRooms());
        HouseBathrooms.setText("Bathrooms: " + house.getBathrooms());
        HouseFlooring.setText("Floring: " + house.getFlooring());
        HousePrice.setText("Price: " + house.getPrice());

        Bitmap bitmap = BitmapFactory . decodeByteArray (house.getImg(), 0, house.getImg().length);
        HouseImage.setImageBitmap(bitmap);
        return view1;
    }
}

Better to follow ViewHolder pattern for better performance.

Related