开发者

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

开发者 https://www.devze.com 2023-03-09 15:24 出处:网络
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'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开发者_StackOverflow, 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消