Android binding SQLite to GridView in Eclipse

I made the following code to retrieve data from a SQLite database.

public Cursor fetchAllScores() { return database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null, null, null); } 

Then I call this function in main.java using the following

 cursor = dbHelper.fetchAllScores(); startManagingCursor(cursor); 

After entering the cursor, I managed to populate myGridView with some data using the following code

 GridView myGV = (GridView)findViewById(R.id.gridView1); String[] cols = new String[] { scoreDbAdapter.KEY_PLAYDATE, scoreDbAdapter.KEY_NUMVALA, scoreDbAdapter.KEY_NUMVALB}; int[] views = new int[] { android.R.id.text1, android.R.id.text2, android.R.id.text2 }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, cols, views); Log.w("NumRows",adapter.getCount() + ""); myGV.setAdapter(adapter); 

Now the problem is only to fill the first row and the first two columns, I want the data to be both in the forst row (2011-10-27, 5, 6), and in the second row (2011-10-26, 3, 2 ) but im getting is only forst row like (2011-10-27, 2011-10-26).

Can this be fixed in a GridView?

+1
source share
2 answers

Create a new layout containing text elements. Find the layout as shown in the first line of the code below and attach your data to the View list. ListView loads a layout containing textView.

 private void populateGrid() { ListView lv = (ListView)findViewById(R.id.listView1); Cursor c = dbHelper.fetchAllScores(); startManagingCursor(cursor); String[] cols = new String[] { TodoDbAdapter.KEY_PLAYDATE, TodoDbAdapter.KEY_NUMVALA, TodoDbAdapter.KEY_NUMVALB}; int[] views = new int[] { R.id.txt_date, R.id.txt_no, R.id.txt_yes}; // Now create an array adapter and set it to display using our row SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listviewtemp, c, cols, views); Log.w("NumRows",adapter.getCount() + ""); lv.setAdapter(adapter); } 

R.layout.listviewtemp is the layout of the listView template (separate xml), lv is the list found in the mainlayout.xml file.

 public Cursor fetchAllScores() { return database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null, null, null); } 
0
source

For your question, see data binding here with AdapterView .

Note. The decription below is just an example of how to bind a cursor to an adapter.

Filling the data layout

Inserting data into a layout is usually done by binding the AdapterView class to an adapter that retrieves data from an external source (possibly a list that is supplied by code or query results from the device database).

The following code example does the following:

1. Creates a Spinner with an existing view and associates it with a new ArrayAdapter, which reads an array of colors from local resources.

2. Creates another Spinner object from the view and associates it with the new SimpleCursorAdapter, which will read the names of people from the device’s contacts (see Contacts.People).

  // Get a Spinner and bind it to an ArrayAdapter that // references a String array. Spinner s1 = (Spinner) findViewById(R.id.spinner1); ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s1.setAdapter(adapter); // Load a Spinner and bind it to a data query. private static String[] PROJECTION = new String[] { People._ID, People.NAME}; Spinner s2 = (Spinner) findViewById(R.id.spinner2); Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null); SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,cur,new String[] {People.NAME}, new int[] {android.R.id.text1}); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s2.setAdapter(adapter2); 

Note that you must have the People._ID column in the projection used with the CursorAdapter, otherwise you will get an exception.

If during the life of your program you change the underlying data that is read by your adapter, you must call notifyDataSetChanged (). This will notify the attached view that the data has been changed and it should be updated.

See this for more details.

Custom CursorAdapters .

Thanks.

0
source

Source: https://habr.com/ru/post/1442977/


All Articles