CursorAdapter不在ListView中显示元素

时间:2018-04-26 12:38:43

标签: android listview simplecursoradapter

我正在尝试使用SimpleCursorColumn将一列SQLITE数据库添加到listview。我在列表视图中获得了准确的行数,但文本没有出现在列表中。我打印出光标的内容,它包含正确的元素。

为什么文字没有出现在列表中?当然,我一直很容易愚蠢。

        public class Tab3Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int LOADER_ID = 42;
    private CursorAdapter _adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab3_frag, container, false);

        ListView lv = view.findViewById(R.id.student_view);
        _adapter = new SimpleCursorAdapter(getContext(),
                R.layout.container_list_item_view, null,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_NAME},
                new int[] { R.id.list_item });
        lv.setAdapter(_adapter);
        getLoaderManager().initLoader(LOADER_ID, null, this);

        return view;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id != LOADER_ID) {
            return null;
        }
        return new CursorLoader(getContext(),
                Contract.Student_Table.CONTENT_URI,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME}, null, null,
                null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        _adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        _adapter.swapCursor(null);
    }
}

tab3_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c2f6ff"
    android:orientation="vertical">

    <ListView
        android:id="@+id/simple_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/student_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black" />

</LinearLayout>

1 个答案:

答案 0 :(得分:5)

更好地使用装载机。请尝试以下示例:

library(shiny)
library(CoreAPIV2)
library(platetools)
library(ggplot2)
library(viridis)

ui <- fluidPage(

  textInput(inputId = "Expbrcode", "Experiment Barcode"),
  plotOutput("PlateMap", click = "PlateMap_click"),
  tableOutput("RawData")
)

server <- function(input, output, session) {
  browser()
  verbose <- FALSE
  api <- CoreAPIV2::coreAPI("template.json")
  con<- CoreAPIV2::authBasic(api,useVerbose=verbose)
  rv <- reactiveValues()

  output$RawData <- renderTable({
    req(input$Expbrcode)
    browser()

    barcode <- input$Expbrcode
    container <- getExperimentContainers(api, "BIOCHEMICAL DOSE RESPONSE EXPERIMENT", barcode)
    RawData <- getExperimentSamplesRawData(api, container$entity, useVerbose = TRUE)$entity
    platemat <- plate_map(as.numeric(levels(RawData$DATA_VALUE))[RawData$DATA_VALUE],num_to_well(c(RawData$CI_CELL)))

    rv$RawData <- cbind(RawData, platemat, 'keepRows' = TRUE)
    })

  output$PlateMap <- renderPlot({
    req(rv$RawData)
    browser()

    ggplot(data = rv$RawData, aes_string(x = "Column", y = "Row")) +
      geom_point(data = expand.grid(seq(1, 12), seq(1, 8)),
                 aes_string(x = "Var1", y = "Var2"),
                 color = "grey90", fill = "white", shape = 21, size = 6, alpha = 0.1) +
      geom_point(aes_string(fill = "values"), colour = "gray20", shape = 21, size = 10) +
      coord_fixed(ratio = (13 / 12) / (9 / 8), xlim = c(0.5, 12.5), ylim = c(0.5, 8.5)) +
      scale_y_reverse(breaks = seq(1, 8), labels = LETTERS[1:8]) +
      scale_x_continuous(breaks = seq(1, 12)) +
      xlab("") +
      ylab("") +

    ggtitle("96-well plate Raw Data") +
    theme_grey() +
    scale_fill_viridis()

  })

  observe({
    req(input$PlateMap_click)
    browser()
    res <- nearPoints(isolate({rv$RawData}), isolate({input$PlateMap_click}), allRows = TRUE)
    rv$RawData$keepRows <- xor(isolate({rv$RawData$keepRows}), res$selected_)
  })

}

shinyApp(ui, server)

container_list.xml:

public class LanguageListActivity extends ListActivity implements
        LoaderCallbacks<Cursor> {
    private static final int LOADER_ID = 42;
    private CursorAdapter _adapter;
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.container_list);
        _adapter = new SimpleCursorAdapter(this,
                R.layout.container_list_item_view, null,
                new String[] { DatabaseConstants.COL_LANG_NAME },
                new int[] { R.id.list_item });
        setListAdapter(_adapter);
        getLoaderManager().initLoader(LOADER_ID, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id != LOADER_ID) {
            return null;
        }
        return new CursorLoader(LanguageListActivity.this,
                LanguageContentProvider.CONTENT_URI,
                new String[] { DatabaseConstants.COL_LANG_ID, DatabaseConstants.COL_LANG_NAME }, null, null,
                null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        _adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        _adapter.swapCursor(null);
  }
}

container_list_item_view.xml:

 <?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />