2016-03-31 9 views
0

アンドロイドのデータベースに連絡先を1つ追加しようとしていますが、AndroidのSQLiteデータベースに単一の名前を追加しようとしていますが、同じ名前の複数のエントリを追加します。

//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 + "]"; 
     } 
} 
+0

'db.insert'の代わりに' db.insertWithOnConflict'を使用してください –

+0

Abhishek Patelはこのメソッドの引数を親切に教えていただけますか?手伝ってくれてありがとう !。 –

+0

はhttp://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.htmlを参照してください。 –

答えて

0

複数回挿入していると思います。 1つのレコードのみを挿入すると、レコードは1つしかありません。 あなたは近いし、再びアプリを開き、それは私が

@Override 
    public void onCreate(SQLiteDatabase db) 
    { 

     String newTableQueryString = "create table " + 
       TABLE_groups + 
            " (" + 
            TABLE_ROW_ID + " integer primary key autoincrement not null," + 
            TABLE_ROW_ONE + " text, " + 
            "UNIQUE ("+TABLE_ROW_ONE+") ON CONFLICT REPLACE" + 
            ");"; 

     db.execSQL(newTableQueryString); 
    } 

のような表を作成し、

public void createGroup(GroupsDetail gd){ 
     ContentValues values = new ContentValues(); 
     // put values in database 
     values.put("group_name" , gd.getName()); 
     db.insertWithOnConflict("groups", null, values, SQLiteDatabase.CONFLICT_REPLACE); 
     // close database transaction 
     db.close(); 
} 
のように挿入し、一度 https://www.dropbox.com/s/9v3ucmaccyvyztm/Sample_2.zip?dl=0

を確認してください作業例 を添付している(複数の挿入)二つのレコード を与えた場合

関連する問題