Skip to content Skip to sidebar Skip to footer

Custom List View With Images And Database Connectivity

I am trying to create an application regarding the custom view which is connected with sqllite database.I consists of images also. The Problem is:When I am trying to execute the pr

Solution 1:

Your adding facebook contact Log.d("Insert: ", "Inserting .."); db.addContact(new Contact("FaceBook", imageInByte)); before showing the list.

You Code Running like this:- refer below points

  • When first time you execute, one facebook added.
  • Again when you execute the program, New facebook contact added. So it shows two contacts.
  • Like wise it gets added and printed, whenever you execute the program

Updated Code:- `

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DataBaseHandlerdb=newDataBaseHandler(this);
    // get image from drawableBitmapimage= BitmapFactory.decodeResource(getResources(),R.drawable.facebook);

    // convert bitmap to byteByteArrayOutputStreamstream=newByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 50, stream);
            byte imageInByte[] = stream.toByteArray();
            /**
             * CRUD Operations
             * */// Inserting Contacts
            Log.d("Insert: ", "Inserting ..");

            //Added Code belowSharedPreferencespreferences= getSharedPreferences("SETTINGS_PREF", Context.MODE_PRIVATE);
            if(preferences.getBoolean("isFirstLaunch", true)){
                db.addContact(newContact("FaceBook", imageInByte));

                SharedPreferences.Editoreditor= preferences.edit();
                editor.putBoolean("isFirstLaunch", false);
                editor.commit();
            }

            // display main List view bcard and contact name// Reading all contacts from database
            List<Contact> contacts = db.getAllContacts();
            for (Contact cn : contacts) {
                Stringlog="ID:" + cn.getID() + " Name: " + cn.getName()
                        + " ,Image: " + cn.getImage();

                // Writing Contacts to log
                Log.d("Result: ", log);
                //add contacts data in arrayList
                imageArry.add(cn);

            }
            adapter = newContactImageAdapter(this, R.layout.screen_list,
                    imageArry);
            ListViewdataList= (ListView) findViewById(R.id.list);
            dataList.setAdapter(adapter);

        }

Solution 2:

try to use this

// Inserting Contacts
                Log.d("Insert: ", "Inserting ..");
                db.addContact(newContact("FaceBook", imageInByte));
                // display main List view bcard and contact name// ArrayList clear() operation removes all elements
                imageArry.clear();


               // Reading all contacts from database
                List<Contact> contacts = db.getAllContacts();

Post a Comment for "Custom List View With Images And Database Connectivity"