Implementing a CursorAdapter to get DB rowID's of ListItems in a ListView

I'm trying to implement a CursorAdapter in my Android App. I read different tutorials and tried out different things but it won't work! I found one question here very similar to mine but I didn't get the idea out of it.

Here's the thing:

I got a Database with multiple tables and foreign keys and so on. I wrote a (tested and working) Database including an extension of the SQLiteHelper class to work properly. The DB-Class containts a lot of methods to get various operations on that DB like: insert, update, delete, alter and some special needs....

My Problem is the following: I have written a method which returns all Data containing (all rows) in the Database which i want to present in a ListView. The returning object is a wrapped ArrayList> . I got the thing working all fine with an ListViewAdapter but then i came to my problem which now almost drives me crazy:

The User shall click a random item in that specific list (which contains all rows from the DB) and then i want to update that specific row in the DB with some new data the user put into a EditText box. Pretty simple task actually but I think I just don't understand the usage of the CursorAdapter.

My questions now:

  1. What does my DB-Method have to return (or look alike) that the CursorAdapter can handle it....
  2. How do I have to implement the Adapter that it just fulfill this one task (retrieving the correct rowID of the DB entry in the list)

Here is my method retrieving the data from the DB

public ArrayList<ArrayList<Object>> getAll() { ArrayList<ArrayList<Object>> allRows = new ArrayList<ArrayList<Object>>(); //Cursorobjekt haelt die Daten einer Zeile und dient dazu in diesen zu iterieren Cursor myCursor1,myCursor2,myCursor3; myCursor1 = db.query ( "Faecher", new String[] { "id" , FACH_NAME, FACH_ART }, null, null, null, null, null ); // Den Pointer an die erste Stelle ruecken myCursor1.moveToFirst(); myCursor2 = db.query ( "Ort", new String[]{"id" , ORT_RAUM , ORT_GEBAEUDE}, null,null,null,null,null,null ); myCursor2.moveToFirst(); myCursor3 = db.query ( "Profs", new String[]{"id", PROFS_NAME, PROFS_SNAME, FKEY_GENDER_ID}, null,null,null,null,null,null ); myCursor3.moveToFirst(); for(int i=0; i < myCursor1.getCount(); i++) { ArrayList<Object> row1 = new ArrayList<Object>(); row1.add(myCursor1.getLong(0)); row1.add(myCursor1.getString(1)); row1.add(myCursor1.getString(2)); row1.add(myCursor2.getLong(0)); row1.add(myCursor2.getString(1)); row1.add(myCursor2.getString(2)); row1.add(myCursor3.getLong(0)); row1.add(myCursor3.getString(1)); row1.add(myCursor3.getString(2)); row1.add(myCursor3.getLong(3)); allRows.add(row1); myCursor1.moveToNext(); myCursor2.moveToNext(); myCursor3.moveToNext(); } myCursor1.close(); myCursor2.close(); myCursor3.close(); return allRows; }

The Adapter is empty right now because my code was real crap and now looks like this hull:

public class SubjectListAdapter extends CursorAdapter { private LayoutInflater myInflater; public SubjectListAdapter(Context context, Cursor c) { super(context, c); } @Override public View newView(Context context, Cursor myCursor, ViewGroup parent) { return null; } @Override public void bindView(View parent, Context context, Cursor myCursor) { } }

I hope someone can help me out with my problem or give me a hint where I have to go to get this working.

-------------Problems Reply------------

If I understand your question correctly, it seems like your primary issue is determining which element has been clicked on within the adapter. You may need to switch from bindview and newview to just using getview. If you do, you can just add an onClicklistener to the view before getView returns it. This way, each row in the list has its own listener and can update the correct row in the db. Any particular information that the onClickListener needs to run the correct database update method (ie the rowId) can also be passed into the listener with a Tag on the view. Hope that helps.

You have to create one big query from DB and get one Cursor. You can use even ordinary sql selection like

String selectQuery = "SELECT id, " FACH_NAME", "+ FACH_NAME ", " + FACH_ART .... +" FROM Faecher INNER JOIN ....";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

to get requared cursor. Than create custom CursorAdapter with getted cursor

public class ExampleCursorAdapter extends CursorAdapter {
public ExampleCursorAdapter(Context context, Cursor c) {
super(context, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView something1 = (TextView)view.findViewById(R.id.something1);
something1.setText(cusror.getString(0));
TextView something2 = (TextView)view.findViewById(R.id.something2);
something1.setText(cusror.getString(1));
.......
viev.addOnClickListener(new OnClickListener{
public void onClick(...){
}
});
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item, parent, false);
return v;
}
}

Method bindView(....) will be called for each raw of Cursor but it will be setted to the different position, you souldnt change it inside this method. Than just bind this adapter to your ListView by setAdapter(....) method.

Category:android Views:1 Time:2011-05-29

Related post

  • How to retrieve rowId of a particular listitem of filtered listview in listActivity displaying SQlite data? 2011-06-11

    Hello Everyone I am new being in ANDROID so I am getting one problem during developing an app using SQLite database. I retrieved some data columns from SQLite database table, stored them in an ArrayList and displayed them using ListActivity. Then I m

  • How to implement different behavior when the user clicks on different views in a ListView? 2011-06-17

    I a new to Android development, so this is kind of a basic question. I would like to implement the same behavior as in the Contacts app. You have a ListView with a series of Contacts | phone icons. There you have one behavior when you click on the co

  • CursorAdapter vs ResourceCursorAdapter 2010-08-10

    What is the exact difference between CursorAdapter and ResourceCursorAdapter? Can somebody explain what are the api's that are required to override when implementing my own ResourceCursorAdapter? I have gone through the ResourceCursorAdapter document

  • CursorAdapter slow/jerky scroll 2011-04-20

    I've got a ListActivity that's using a CursorAdapter to draw a dataset in a table format. I originally used a TableLayout (and drew the entire table) but I was concerned that this was using too much memory (and drawing the page was slow) so I'm now i

  • Make a ListView row clickable using a custom CursorAdapter 2011-01-24

    I am using a Custom CursorAdapter that overrides newView() and bindView() to show a custom ListView. I am inflating the row layout xml file and binding a OnClickListener to the view passed as argument to bindView(): public void bindView(View view, Co

  • Cursor not binding text correctly with custom adapter 2012-01-17

    My Custom Adapter that extends SimpleCursorAdapter for my ListFragment does not display the items in my database correctly. it does not display the text in the TextView, what else do I have to do to the cursor for it to show the correct text? I thoug

  • Strange out of memory issue while loading an image to a Bitmap object 2009-01-25

    I have a list view with a couple of image buttons on each row. When you click the list row, it launches a new activity. I have had to build my own tabs because of an issue with the camera layout. The activity that gets launched for result is a map. I

  • How can I add event handling in each row in ListView 2009-12-01

    I create a CursorAdapter to provide data for my ListView. I implement the bindView() method to show data in a row of my Listview. But at the end of my bindView, I add an clickListener to it. But when I run it on emulator, I don't see any print statem

  • Reuse default android contact search in 3rd party application - Possible? 2010-07-14

    At the moment I am trying to evaluate if it is possible to reuse the android contact search in a 3rd party application. I got the problem that I have to implement a contact list/picker including a filtering possibility for the contacts list. That mea

  • Understanding BaseAdapters and how to use them 2011-01-25

    I'm trying to set up a an image GridView Layout, and this involves deriving a new class from the BaseAdapter class. I have been using the tutorial on the website developer.android.com, but I still don't quite understand what it means. Could someone p

  • android: Refresh ListView using ListAdapter and SimpleCursorAdapter 2011-02-25

    I'm trying to refresh a ListView that uses a ListAdapter created as a SimpleCursorAdapter. Here is my code for creating the Cursor and ListAdapter in onCreate which populates the ListView. tCursor = db.getAllEntries(); ListAdapter adapter=new SimpleC

  • How we can drag an image from one table to another in android 2011-04-08

    I am newbie to Android. Can we move one imageview from a table layout to another layout? If so how it can be done? I had created an XML based view. In that view I created two table layout. Inside one table I place an image. Then I need to drag that i

  • How can I adapt a cursor to a ListView with a lot of custom behavior? 2011-05-07

    I'm currently working on an android application that uses a SimpleCursorAdapter with a custom ViewBinder to set specific text color and image visibility on items in my ListView. However, when my list is longer then the size of a screen, and the user

  • ListView empty if SimpleCursorAdapter closed() 2011-10-26

    I have a problem understanding why is my ListActivity and its corresponding listView empty when I call close() method on cursor object? Let me explain myself a bit... I retreive some values from a DB and get the result in my cursor object. After that

  • select and delete a record without the information of primary key 2011-11-03

    My requirement is to select a record first (the record may not have a primary key) and the delete the same record. Below is the flow : Select all records from table. while (resultset.next()), get the single record. delete the record. The code has to

  • Using listview in android 2012-02-10

    I am making an android application that needs to use a ListView. I want to add a menubutton that says "Add to list" and once the user presses that menubutton, it pops up a popupwindow containing a TextView, EditText and two Buttons, "Ok" and "Cancel"

  • jqGrid: Do not allow selecting a row in the grid without the others are updated 2012-03-05

    I'm working with two grids. When a line of the first grid is selected, the second grid is reloading. So,I wanted it was not possible to select a line of the first grid while the second is not reloading. --------------Solutions------------- I am not f

  • How to avoid thousands of needless ListView.SelectedIndexChanged events? 2008-09-17

    If a user select all items in a .NET 2.0 ListView, the ListView will fire a SelectedIndexChanged event for every item, rather than firing an event to indicate that the selection has changed. If the user then clicks to select just one item in the list

  • Force resize of GridView columns inside ListView 2009-05-10

    I have a ListView WPF control with a GridView. I'd like to resize the GridView columns when the content of the columns changes. I have several distinct data sets but when I change from one to another, the size of each columns fits the previous data.

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.118 (s). 11 q(s)