2016-07-14 32 views
-1
//Here is the code for Content provider. It has two uri's //1)content://authority/student(table name) and //2)content://authority/student/_id to identify each student. 


public class StudentProvider extends ContentProvider { 
    public static final int STUDENT=0; 
    public static final int STUDENT_ID=1; 
    private static HashMap<String, String> STUDENTS_PROJECTION_MAP; 
    public StudentHelper helper; 
    UriMatcher match=buildUrimatcher(); 
    static UriMatcher buildUrimatcher() 
    { 
     UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH); 
     matcher.addURI(StudentContract.CONTENT_AUTHORITY,StudentContract.PATH,STUDENT); 
     matcher.addURI(StudentContract.CONTENT_AUTHORITY,StudentContract.PATH+"/#",STUDENT_ID); 
     return matcher; 
    } 

    @Override 
    public boolean onCreate() { 

     helper=new StudentHelper(getContext()); 
     return true; 
    } 

    @Override 
    public Cursor query(Uri uri, String[] strings, String s, String[] strings2, String s2) { 

     SQLiteDatabase database=helper.getReadableDatabase(); 
     SQLiteQueryBuilder builder=new SQLiteQueryBuilder(); 
     builder.setTables(StudentContract.Student.Table); 
     int id=match.match(uri); 
     switch (id) 
     { 

      case STUDENT: 
      { 
       builder.setProjectionMap(STUDENTS_PROJECTION_MAP); 
       break; 
      } 
      case STUDENT_ID: 
      { 
       builder.appendWhere(StudentContract.Student._ID+"="+uri.getPathSegments().get(1)); 
       break; 
      } 
      default: 
       throw new IllegalArgumentException("Unknown URI " + uri); 
     } 
     Cursor cursor=builder.query(database,strings,s,strings2,null,null,s2); 
     cursor.setNotificationUri(getContext().getContentResolver(),uri); 
     return cursor; 
    } 

    @Override 
    public int bulkInsert(Uri uri, ContentValues[] values) { 
     SQLiteDatabase database=helper.getWritableDatabase(); 
     int id=match.match(uri); 
     switch (id) 
     { 
      case STUDENT: 
      { 
       database.beginTransaction(); 
       int count=0; 
       try 
       { 
        for(ContentValues value:values) 
        { 
         long rid= database.insert(StudentContract.Student.Table,null,value); 
         if(rid!=-1) 
         { 
          count++; 
          Log.d("Bulk insert","success"); 
         } 
        } 
        database.setTransactionSuccessful(); 
       }finally { 
        database.endTransaction(); 
       } 
       getContext().getContentResolver().notifyChange(uri,null); 
       return count; 
      } 
      default:return super.bulkInsert(uri, values); 
     } 


    } 


} 

//Here is the code of Activity which inserts records into database from an //array. Im inserting records into database using bulk insert method. 

//將記錄插入數據庫的代碼。每次運行程序時都會將記錄插入到數據庫中。 SQLitedatabase

final View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      String[] names=new String[]{"sai","kiran","seenu","akhil","devi","sanath","patro","patch","reddy","sai kiran"}; 
      ListView listView= (ListView) rootView.findViewById(R.id.list_view); 
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
        Cursor cursor= (Cursor) adapterView.getItemAtPosition(i); 
        Uri Student= ContentUris.withAppendedId(StudentContract.Student.CONTENT_URI,cursor.getInt(cursor.getColumnIndex(StudentContract.Student._ID))); 
        Toast.makeText(getActivity(),Student.toString(),Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      Cursor c=getActivity().getContentResolver().query(StudentContract.Student.CONTENT_URI,null,null,null,null); 
      cursorAdapter =new TodoCursorAdapter(getActivity(),c,0); 

      String[] grades=new String[]{"A","A1","B","C","A1","C","D","D","B","A"}; 
      Vector<ContentValues> valuesVector=new Vector<ContentValues>(names.length); 
      for(int i=0;i<10;i++) 
      { 
       ContentValues values=new ContentValues(); 
       values.put(StudentContract.Student.name,names[i]); 
       values.put(StudentContract.Student.grade,grades[i]); 
       valuesVector.add(values); 
      } 
      if(valuesVector.size()>0) 
      { 
       ContentValues[] valueses=new ContentValues[valuesVector.size()]; 
       valuesVector.toArray(valueses); 
       getActivity().getContentResolver().bulkInsert(StudentContract.Student.CONTENT_URI,valueses); 

      } 


      listView.setAdapter(cursorAdapter); 

每次我執行程序時,10條記錄再次插入數據庫。每次程序執行時,我都不想插入記錄。 Im初學者在這個領域的任何幫助表示讚賞。 StudentHelper類擴展了Sqliteopenhelper,它包含oncreate()和onupgrade()方法

回答

0

這是爲什麼?

for(int i=0;i<10;i++) { 
    ContentValues values=new ContentValues(); 
    values.put(StudentContract.Student.name,names[i]); 
    values.put(StudentContract.Student.grade,grades[i]); 
    valuesVector.add(values); 
} 
相關問題