G
G
goodTimes2017-03-16 12:14:38
Java
goodTimes, 2017-03-16 12:14:38

Why does it give an error that the adapter is empty?

Here is the addition to the table

String tmp = editText.getText().toString();

                    dbHelper = new DbHelper(view.getContext());
                    db = dbHelper.getWritableDatabase();
                    db.execSQL("INSERT INTO " + DbHelper.TABLE_USERS +" ( "+ DbHelper.KEY_NAME + " ) VALUES ('"+ tmp + "');");
                    db.close();

Here is reading from this table in listView in another fragment
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_result, container, false);
        listView = (ListView) view.findViewById(R.id.listRes);

        dbHelper = new DbHelper(view.getContext());
        db = dbHelper.getReadableDatabase();

        int i = 0;
        Cursor c = db.rawQuery("Select * FROM " + DbHelper.TABLE_USERS, null);
        if(c!=null&&c. moveToFirst()){
            do{
                String name  = c.getString(c.getColumnIndexOrThrow (DbHelper.KEY_NAME)) ;
                informations[i] = name;
                i++;
            }while(c.moveToNext());
        }

        
        ArrayAdapter<String> adapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, informations);
        listView.setAdapter(adapter);

        db.close();
        return view;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-03-16
@zagayevskiy

If I were you, I would try replacing view.getContext() with this.getContext(). view has not yet been attached, it is not a fact that it has a context at all, most likely not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question