How to create ListView with SQLite data in Fragment

0

How do I insert the SQLite data into this listview of my code:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        String[] sCheeseStrings = {"Emerson","Simone","Samara"};
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,sCheeseStrings));
    }

The data I want to display is 'TITLE' and 'AUTHOR'.

I'm trying this:

public class TabActivity extends Fragment { 
    private final DataBaseHandler db = new DataBaseHandler(getActivity());
    public class TabActivity extends Fragment {
    private final DataBaseHandler db = new DataBaseHandler(getActivity());

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        db.addBook(new Book("Android Application Development Cookbook", "Wei Meng Lee"));   
        db.addBook(new Book("Android Programming: The Big Nerd Ranch Guide", "Bill Phillips and Brian Hardy"));       
        db.addBook(new Book("Learn Android App Development", "Wallace Jackson"));


        View rootView = inflater.inflate(R.layout.activity_tab, container, false);

        return rootView;
        List<Book> cursor = db.getAllBooks();

        String title;
        String author;
        String[] from = { title, author }; 
        int[] to = { android.R.id.text1, android.R.id.text1 }; 

        SimpleCursorAdapter adapter = SimpleCursorAdapter( getActivity().getApplicationContext(), android.R.layout.simple_list_item_2, cursor, from, to);
    }
}

But it is giving an error "The method SimpleCursorAdapter (Context, int, List, String [], int []) is undefined for the TabActivity type". What can it be?

    
asked by anonymous 10.06.2014 / 01:27

1 answer

1

If you want you can use SimpleCursorAdapter

String[] from = { id, titulo }; // Colunas que você quer exibir
int[] to = { android.R.id.text1, android.R.id.text1 }; // ID's dos componentes que estão configurados no recurso de layout que será passado para o construtor do SimpleCursorAdapter.

Cursor cursor = retornaMeuCursor(); // Aqui você faz a busca no banco de dados e retorna o seu cursor com os dados.

SimpleCursorAdapter adapter = SimpleCursorAdapter( getActivity().getApplicationContext(), android.R.layout.simple_list_item_2, cursor, from, to);
    
10.06.2014 / 03:52