1

I am trying to save data into a database and have to display it in a listview. I am able to get output but its not what i required its just showing URI.Kindly help me with how can i get my COLUMN_NAME into the listView adapter from databse

Database code:

public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Mydatabase";
public static final String TABLE_CONTACTS = "MyContacts";
public static final String COLUMN_NAME= "name";
public static final String COLUMN_NUMBER= "number";
public static final String COLUMN_EMAIL= "email";
public static final String COLUMN_CONTACT_ID = "id";

public static final String TABLE_GROUPS = "Groups";
public static final String COLUMN_GROUP_ID = "id";
public static final String COLUMN_GRPNAME= "name";
public static final String COLUMN_MEMBERS= "number";



public DBHelper(Context context)
{
    super(context, DATABASE_NAME, null, 1);
}


@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    Log.d("INFO1","creating db");
    db.execSQL("create table " + TABLE_CONTACTS +
            " (" + COLUMN_CONTACT_ID + " integer primary key, " + 
COLUMN_NAME + " text, " + COLUMN_NUMBER + " number, " + COLUMN_EMAIL + " 
text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    onCreate(db);
}



public boolean addcontact(Database_contacts database_contacts){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    /*contentValues.put(COLUMN_CONTACT_ID, database_contacts.getId());*/
    contentValues.put(COLUMN_NAME, database_contacts.getName());
    contentValues.put(COLUMN_NUMBER, database_contacts.getNumber());
    contentValues.put(COLUMN_EMAIL, database_contacts.getEmail());

    db.insert(TABLE_CONTACTS, null, contentValues);
    db.close();
    return true;
}

public List<Database_contacts> getAllcontactDetails() {

    List<Database_contacts> contactList = new ArrayList<Database_contacts>
();

    String selectQuery = "SELECT "+COLUMN_NAME+" FROM " + TABLE_CONTACTS;

    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()) {
        do {
            Database_contacts dashboardDetails = new Database_contacts();

/*dashboardDetails.setId(Integer.parseInt(cursor.getString(0)));*/
            dashboardDetails.setName(cursor.getString(0));
            contactList.add(dashboardDetails);
        } while (cursor.moveToNext());
    }
    return contactList;
}

}

contactsactivity code:

public class Contact extends MainActivity {
Button addcontact;
DBHelper database;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
    final ListView myList= (ListView) findViewById(R.id.listview);
    database = new DBHelper(this);
    List<Database_contacts> contactList = database.getAllcontactDetails();
    contactList.toArray();
    ArrayAdapter<Database_contacts> myAdapter=new 
 ArrayAdapter<Database_contacts>(this, android.R.layout.simple_list_item_1,
            contactList);
    myList.setAdapter(myAdapter);

    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(getApplicationContext(),
                    "Click ListItem Number " + position, Toast.LENGTH_LONG)
                    .show();
        }
    });

    addcontact = (Button)findViewById(R.id.addcontact);
    addcontact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new 
Intent(getApplicationContext(),AddContact_activity.class);
            startActivity(intent);
        }
    });
}
}
5
  • Ur question is not clear to me. what you want to display in ListView? Commented May 14, 2015 at 10:35
  • you want to display names in listView ? Commented May 14, 2015 at 10:36
  • yes i have mentioned above itself i need to show COLUMN_NAME from database into the contactlist in my contactactivity class Commented May 14, 2015 at 10:44
  • http://stackoverflow.com/questions/2382528/how-to-get-a-tables-columns-arraylist-on-android Commented May 14, 2015 at 11:05
  • here i am using two different classes M D, i have seen that direct method but its not of much use for me........kindly mention any method which involves having database and your activity in different classes and I am getting some thing like this in the list view "com.example.rknikhil.contacts.Database_contacts@ea65f53 " Commented May 15, 2015 at 4:50

1 Answer 1

0

I think you need to use a adapter, possibly custom(?) extending from CursorAdapter for retrieving data from your database, and some sql statements. And what du you mean with COLUMN_NAME, would you like to show the actual name of the COLUMN_NAME as in "name" or the content of it?

Sign up to request clarification or add additional context in comments.

1 Comment

yes COLUMN_NAME is the field name where i store my name inside db

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.