Hello everyone !
Dear all, I am trying to add one contact in database of android butt it add multiple entries with same name, kindly help me out as I am new in this filed. Thanks in advance for helping me out.
//Function to add single name/group name in database class
public void createGroup(GroupsDetail gd){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
// put values in database
values.put(group_NAME , gd.getName());
db.insert(table_Group , null , values);
// close database transaction
db.close();
}
//Function to get all names/group names in database class
public List<GroupsDetail> getallcontacts(){
List<GroupsDetail> groupList = new ArrayList<GroupsDetail>();
String query = "SELECT * From " + table_Group;
//get referance of ContactAS database
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
GroupsDetail group = null;
if(cursor.moveToFirst()){
do{
group = new GroupsDetail();
group.setId(Integer.parseInt(cursor.getString(0)));
group.setName(cursor.getString(1));
groupList.add(group);
}while(cursor.moveToNext());
}
return groupList;
}
//Code of main class
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Groups extends Activity{
Button Addgroup,Delgroup;
ListView groupnames;
GroupsDetail gd;
ArrayList<String> listItems;
List<GroupsDetail> gdlist;
Groupsdb gdb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.groups);
Addgroup = (Button)findViewById(R.id.addgroup);
Delgroup = (Button)findViewById(R.id.deletegroup);
groupnames = (ListView)findViewById(R.id.listofgroups);
groupnames.setLongClickable(true);
gdb = new Groupsdb(this);
listItems = new ArrayList<String>();
gdlist = new ArrayList<GroupsDetail>();
gd = new GroupsDetail("Family");
gdb.createGroup(gd);
gdlist = gdb.getallcontacts();
for(GroupsDetail cn : gdlist){
listItems.add(cn.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.text, listItems);
groupnames.setAdapter(adapter);
}
}
//code of GroupDetails class
public class GroupsDetail {
private String Name;
private int id;
public GroupsDetail(){}
public GroupsDetail(String name){
super();
this.Name = name;
}
public GroupsDetail(String name, int i){
super();
this.Name = name;
this.id = i;
}
public int getId(){
return id;
}
public void setId(int i){
this.id = i;
}
public String getName(){
return Name;
}
public void setName(String name){
this.Name = name;
}
public String toString(){
return "Group [id = " + id + ", Name = "+ Name + "]";
}
}
Aucun commentaire:
Enregistrer un commentaire