How to create Listview with Sqlite data in Fragment

Asked

Viewed 1,324 times

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 to do that:

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 type Tabactivity" . What can it be? inserir a descrição da imagem aqui

  • Amigo @Emerson Barcellos, you are giving a Return command in the method onCreateView() before executing the instructions that create your SimpleCursorAdapter. Put the Return statement to the end of the method and the cursor creation before the instruction return rootView. If my answer has helped, mark it as accepted.

  • Another thing. Look at the questions you asked in your profile and see if you have any pending. Any questions you asked and forgot to answer if you were able to find the answer yourself or if someone answered. Try to avoid asking questions and letting them drop without your return. This keeps the Stack Overflow site good.

  • Wow @Lucassantos I’m almost giving up using Fragment, in Tabhost and Activity I have a working app, and I want to change, but I don’t know how to use the damn Fragment. Well come on...I’ll put the error print, why does it give error?

  • 1

    why are you using Tabactivity? This class is obsolete.

  • OK @Lucassantos thank you so much. I will look for alternatives to solve my problem

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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.