0

I'm not good at using Custom Listview but I want to display my SQLite data into it.When I try it doesn't show me any thing in List.

thanks, guys to tell me what is the problem in advance:

Rq: Retrieving data works fine And my table has 3 rows : enter image description here

my source code:

new_facture.java:

Databasehelper mydatabase;
    ArrayAdapter<String> adapter;
    ArrayList<String> itemList;
    ArrayList<Product> arrayList;
    CustomAdaptorLP customAdaptorLP;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_facture);
        ListView LP = (ListView)findViewById(R.id.productLV);
   mydatabase = new Databasehelper(this);///-----Inialisation de base
        arrayList = new ArrayList<>();
        arrayList = mydatabase.getallProductdataintolist();
        customAdaptorLP = new CustomAdaptorLP(this,arrayList);
        Product product = new Product();
            Log.d("stat", " offline mode");
            LP.setAdapter(customAdaptorLP);

CustomAdaptorLP.java::


        private Activity activity;
            private ArrayList<Product> Items;
            private LayoutInflater inflater;
            TextView tvId,tvname,tvprix;
            Product sm;
            public CustomAdaptorLP(Activity activity, ArrayList<Product> items) {
                this.activity = activity;
                this.Items = items;
            }

            @Override
            public int getCount() {
                return 0;
            }

            @Override
            public Product getItem(int position) {
                return Items.get(position);
            }

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

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if(inflater == null){
                    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }
                if(convertView==null){
                    convertView = inflater.inflate(R.layout.lp_layout,null);
                }
                tvId = (TextView)convertView.findViewById(R.id.id_product);
                tvname = (TextView)convertView.findViewById(R.id.name_product);
                tvprix = (TextView)convertView.findViewById(R.id.prix_product);
                sm = Items.get(position);
                tvId.setText(sm.id);
                tvname.setText(sm.produit);
                tvprix.setText(sm.prix);
                return convertView;
            }


    and Product is a class has 3 Strings id/produit/prix with GET/SET and those two constructs:

    public Product(String id, String produit, String prix) {
            this.id = id;
            this.produit = produit;
            this.prix = prix;
        }

        public Product() {

        }

Databasehelper.java

public ArrayList<Product> getallProductdataintolist(){

        ArrayList<Product> list = new ArrayList<>();
        String sql = "select * from " + Table2;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db .rawQuery(sql,null);
        if(cursor.moveToFirst()){
            do{
                Product sm = new Product();
                sm.setId(cursor.getString(0));
                sm.setProduit(cursor.getString(1));
                sm.setPrix(cursor.getString(2));


            }while (cursor.moveToNext());
        }

        return list;
    }

1 Answer 1

2

I think you return 0 in getCount() method instead Items.size(); change your Adapter class method like below.

@Override
    public int getCount() {
        return Items.size();
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but still don't work and I changed from CustomAdapterLP.java sm = Items.get(position); tvId.setText(sm.getId());``tvname.setText(sm.getProduit()); tvprix.setText(sm.getPrix()); but not works
@bbazeae Are you sure your ArrayList<Product> Items not empty?
I found that the return of getallProductdataintolist() list: size = 0 I feel like my problem is Product sm = new Product();
@bbazeae I think you need to add Product object in in list in getallProductdataintolist() in while loop e.g list.add(sm);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.