作品:
<FlatList
data={['0', '1', '2', '3', '4']}
renderItem={({ item }) => (
<Button title={item} />
)}
/>
不起作用(没有渲染):
<FlatList
data={[...Array(5).keys()].map(String)}
renderItem={({ item }) => (
<Button title={item} />
)}
/>
可能是什么原因?
答案 0 :(得分:0)
它实际上有效,请查看下面的演示
https://snack.expo.io/H1elODwPb
代码
public class TaskProvider extends ContentProvider {
private static final String TAG = "TaskProvider";
private static final int TASKS=100;
private static final int TASK_ID=101;
private static final UriMatcher URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
static {
URI_MATCHER.addURI(TaskContract.CONTENT_AUTHORITY,TaskContract.PATH_TASKS,TASKS);
URI_MATCHER.addURI(TaskContract.CONTENT_AUTHORITY,TaskContract.PATH_TASKS+"/#",TASK_ID);
}
private TaskDBHelper taskDBHelper;
@Override
public boolean onCreate() {
taskDBHelper=new TaskDBHelper(getContext());
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
SQLiteDatabase sqLiteDatabase=taskDBHelper.getReadableDatabase();
Cursor cursor;
int match=URI_MATCHER.match(uri);
switch (match){
case TASKS:
cursor=sqLiteDatabase.query(TaskContract.TaskEntry.TABLE_NAME,projection,selection,
selectionArgs,null,null,sortOrder);
break;
case TASK_ID:
selection= TaskContract.TaskEntry._ID+"=?";
selectionArgs=new String[]{String.valueOf(ContentUris.parseId(uri))};
cursor=sqLiteDatabase.query(TaskContract.TaskEntry.TABLE_NAME,projection,selection,
selectionArgs,null,null,sortOrder);
break;
default:
throw new IllegalArgumentException("Cannot query Unknown Uri "+uri);
}
cursor.setNotificationUri(getContext().getContentResolver(),uri);
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
final int match = URI_MATCHER.match(uri);
switch (match) {
case TASKS:
return TaskContract.TaskEntry.CONTENT_LIST_TYPE;
case TASK_ID:
return TaskContract.TaskEntry.CONTENT_ITEM_TYPE;
default:
throw new IllegalStateException("Unknown URI " + uri + " with match " + match);
}
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
final int match=URI_MATCHER.match(uri);
switch (match){
case TASKS:
return insertTask(uri,values);
default:
throw new IllegalArgumentException("Insertion is not supported for "+uri);
}
}
private Uri insertTask(Uri uri, ContentValues values) {
String name=values.getAsString(TaskContract.TaskEntry.NAME);
if(name==null){
throw new IllegalArgumentException("Task Name is required");
}
/* String duedate=values.getAsString(TaskContract.TaskEntry.DUEDATE);
if(duedate==null){
throw new IllegalArgumentException("DUEDATE is required");
}
String duetime=values.getAsString(TaskContract.TaskEntry.DUETIME);
if(duetime==null){
throw new IllegalArgumentException("DUETIME is required");
}*/
SQLiteDatabase sqLiteDatabase=taskDBHelper.getWritableDatabase();
long id=sqLiteDatabase.insert(TaskContract.TaskEntry.TABLE_NAME,
null,values);
if(id==-1){
return null;
}
getContext().getContentResolver().notifyChange(uri,null);
return ContentUris.withAppendedId(uri,id);
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase database = taskDBHelper.getWritableDatabase();
// Track the number of rows that were deleted
int rowsDeleted;
final int match = URI_MATCHER.match(uri);
switch (match) {
case TASKS:
// Delete all rows that match the selection and selection args
rowsDeleted = database.delete(TaskContract.TaskEntry.TABLE_NAME, selection, selectionArgs);
break;
case TASK_ID:
// Delete a single row given by the ID in the URI
selection = TaskContract.TaskEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
rowsDeleted = database.delete(TaskContract.TaskEntry.TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
// If 1 or more rows were deleted, then notify all listeners that the data at the
// given URI has changed
if (rowsDeleted != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
// Return the number of rows deleted
return rowsDeleted;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
final int match=URI_MATCHER.match(uri);
switch (match){
case TASKS:
return updateTask(uri,values,selection,selectionArgs);
case TASK_ID:
selection = TaskContract.TaskEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return updateTask(uri, values, selection, selectionArgs);
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
}
private int updateTask(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
if (values.containsKey(TaskContract.TaskEntry.NAME)) {
String name = values.getAsString(TaskContract.TaskEntry.NAME);
if (name == null) {
throw new IllegalArgumentException("Task requires a name");
}
}
if (values.containsKey(TaskContract.TaskEntry.DUEDATE)) {
String duedate = values.getAsString(TaskContract.TaskEntry.DUEDATE);
if (duedate == null) {
throw new IllegalArgumentException("Task requires a duedate");
}
}
if (values.containsKey(TaskContract.TaskEntry.DUETIME)) {
String duetime = values.getAsString(TaskContract.TaskEntry.DUETIME);
if (duetime == null) {
throw new IllegalArgumentException("Task requires a duetime");
}
}
// If there are no values to update, then don't try to update the database
if (values.size() == 0) {
return 0;
}
// Otherwise, get writeable database to update the data
SQLiteDatabase database = taskDBHelper.getWritableDatabase();
// Perform the update on the database and get the number of rows affected
int rowsUpdated = database.update(TaskContract.TaskEntry.TABLE_NAME, values, selection, selectionArgs);
// If 1 or more rows were updated, then notify all listeners that the data at the
// given URI has changed
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
// Return the number of rows updated
return rowsUpdated;
}
}